2015-01-10 352 views
0

我正在使用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> 
+0

Ofcourse它將作爲表單提交給'../ home/homePage'而不是處理登錄的URL。 –

+0

@ M.Deinum:我已經爲控制器中的../home/homePage提供了請求映射。 –

+0

當你點擊登錄按鈕時,你期待它做什麼? – Aeseir

回答

0

所以,你現在有什麼問題以及你如何期待它的工作。首先,您需要將您的登錄頁面指向將處理登錄表單的url(如果需要,您可以自定義)。

其次,如果你希望用戶在「/家/主頁」到最後總是那麼你應該只需添加loginSuccessURL("/home/homePage");

否則,你可以做什麼,只是設置antMatcher(「/家/主頁」 )具有預期的角色,並且除非用戶通過身份驗證,否則它始終會請求登錄。

+0

完成.. !! thanx人:) –

相關問題