Spring security : 登录后重定向到登录页面

Spring security : redirecting to the login page after loggin in

我正在使用 java 配置实现 spring 安全性。

在此处提供必要的配置 类。

SpringSecurity.java

@Configuration
@EnableWebSecurity
public class SpringSecurity extends WebSecurityConfigurerAdapter {

@Autowired
private AuthenticationSuccessHandler authenticationSuccessHandler;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("password").roles("IS_AUTHENTICATED_ANONYMOUSLY");
}

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/**");
}

protected void configure(HttpSecurity http) throws Exception {

    http.csrf().disable().authorizeRequests().antMatchers("/auth/login").permitAll()
            .anyRequest().authenticated()
            .and().formLogin().loginPage("/auth/login")
            .usernameParameter("j_username").passwordParameter("j_password")
            .permitAll().successHandler(authenticationSuccessHandler)
            .and().httpBasic();

}

}

WebConfig.java

public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer{

@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[] { SpringConfig.class,SpringSecurity.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
    // TODO Auto-generated method stub
    return null;
}

@Override
protected String[] getServletMappings() {
    // TODO Auto-generated method stub
    return new String[] {"/"};
}

}

AuthenticationSuccessHandler.java

@Component
public class AuthenticationSuccessHandler implements org.springframework.security.web.authentication.AuthenticationSuccessHandler{

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication) throws IOException,
        ServletException {


    System.out.println(authentication.getName());
    redirectStrategy.sendRedirect(request, response, "/home/homePage");
}

 }

SpringConfig.java 是定义所有数据源和其他包扫描相关内容的地方,我想这里不需要。

问题是,当点击登录页面 url (contextPath)/auth/login 时,它会显示登录页面。但, 在我点击登录按钮后,它会将我重定向到相同的登录页面。

我在这里提供login.jsp。

<form:form action="../home/homePage" class = "form-horizontal">
    <legend id = "loginLegend">LOGIN</legend>
    <hr style="border: none; height: 1px; color: blue; background: #244363;"/>
        UserName: <br>
        <input type="text" class="form-control" name="j_username" style = "width: 90% !important; margin-left : 20px;"/><br>
        Password:<br>
        <input type="password" class = "form-control" name="j_password" style = "width: 90% !important;margin-left : 20px;"/><br>
        <button id = "loginButton" class="btn btn-primary" type="submit">Login</button>
</form:form>

关于您现在拥有的以及您期望它如何工作的一些问题。

首先,您需要将登录页面指向将处理登录表单的 url(如果需要,您可以自定义)。

其次,如果您希望用户始终以“/home/homePage”结束,那么您应该添加 loginSuccessURL("/home/homePage");

否则你可以做的,只是设置 antMatcher("/home/homePage") 与预期的角色,除非用户通过身份验证,否则它将始终请求登录。