Spring安全性:登录后重定向到登录页面

时间:2015-01-10 07:56:47

标签: spring spring-security

我正在使用java配置实现spring security。

在这里提供必要的配置类。

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>

1 个答案:

答案 0 :(得分:0)

所以你现在拥有的一些问题以及你希望它如何发挥作用。

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

其次,如果您希望用户始终在&#34; / home / homePage&#34;然后你应该添加loginSuccessURL("/home/homePage");

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

相关问题