2016-01-12 135 views
2

我正在使用Java配置而不是xml,使用Eclipse,Maven和Spring構建Spring MVC應用程序webmvc ver 4.2.4和Spring security 4.0.3。我有它在Tomcat 7上運行。 有多個jsp可以從一個導航到另一個,所以@RequestMappings是正確的(它們在下面的configure()方法中列出)。我使用log4j設置了所有可能的日誌記錄,所以我可以看到我的配置和控制器正在被調用。在啓動過程中,日誌文件顯示正在設置的映射:如何使用Java配置登錄頁面配置Spring MVC HttpSecurity

... RequestMappingHandlerMapping - 映射 「{[/登錄],方法= [GET]}」 ...... ... RequestMappingHandlerMapping - 映射「{ [/登錄],方法= [POST]}「......

我的問題是在登錄時得到提交它的屏幕不會發布在LoginController中類正確的方法,它一直要到」 init「方法,該方法爲GET請求註釋。

這裏是SecurityConfig

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled=true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

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

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().antMatchers("/","/register","/about","/home","/demo").permitAll() 
    //.loginProcessingUrl("/login") 
    .antMatchers(HttpMethod.POST,"/login").permitAll() 
    .anyRequest().authenticated() 
    .and().formLogin().permitAll().loginPage("/login") 
    .and().logout().logoutUrl("/logout").invalidateHttpSession(true).logoutSuccessUrl("/home"); 
    } 
} 

當//.loginProcessingUrl("/login「)是取消註釋,自動生成的春天登錄表單出現,我可以登錄!所以它適用於默認表單,但不適用於我的表單。

LoginController.java看起來是這樣的:

@Controller 
public class LoginController { 
    private Logger logger = Logger.getLogger(LoginController.class); 

    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public String init(Locale locale, Model model) { 
    logger.info("LoginController login INIT!"); 
    return "login"; 
    } 

    @RequestMapping(value = "/login", method = RequestMethod.POST) 
    public String login(@ModelAttribute LoginDTO loginObject, Locale locale, Model model) { 
    logger.info("LoginController login POST!"); 
    return "home"; 
    } 

} 

當Spring的默認登錄頁面提交,但不映射到的LoginController。當我的login.jsp被提交時,請求轉到init()方法,而不是映射到POST的login()方法。

我的自定義的login.jsp的片段我想使用的,而不是默認的jsp:

<form:form action="${loginProcessingUrl}" method="post"> 
     <p> 
     <label for="username">Username</label> 
     <input type="text" id="userId" name="userId"/> 
     </p> 
     <p> 
     <label for="password">Password</label> 
     <input type="password" id="password" name="password"/> 
     </p> 
     <div> 
      <button type="submit">Log in</button> 
     </div> 
</form:form> 

該框架添加CSRF令牌的登錄頁面,我可以看到在瀏覽器上,這似乎是工作,但我不知道是否重要。

<input type="hidden" name="_csrf" value="ac81daad-f2e4-4357-a7a8-b7b10a67036f"> 

我剛學春,並已對有關Spring Security的工作原理和配置,尤其是與所有的鏈接方法HTTP對象一些深入的解釋到處搜尋。如果任何人都可以指導我參考最新的Spring Security,那麼我會很感激,或者讓我知道我的代碼需要什麼。

回答

3

從我的理解,您應該刪除您控制器@RequestMapping(value = "/login", method = RequestMethod.POST)和改變安全配置如下:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled=true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .inMemoryAuthentication() 
     .withUser("user").password("password").roles("USER"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
     .antMatchers("/", "/register", "/about", "/home", "/demo").permitAll() 
     .anyRequest().authenticated() 
     .and() 
    .formLogin() 
     .loginPage("/login") 
     .loginProcessingUrl("/login") 
     .permitAll() 
     .and() 
    .logout() 
     .logoutUrl("/logout") 
     .logoutSuccessUrl("/home") 
     .invalidateHttpSession(true); 
    } 
} 

Spring將自動春季安全過濾器處理您的POST請求,並重定向到您的登錄表單如有必要。請確保您登錄的字段名稱是正確命名「用戶名」和「密碼」:

<form:form action="${loginProcessingUrl}" method="post"> 
    <p> 
    <label for="username">Username</label> 
    <input type="text" id="username" name="username"/> 
    </p> 
    <p> 
    <label for="password">Password</label> 
    <input type="password" id="password" name="password"/> 
    </p> 
    <div> 
     <button type="submit">Log in</button> 
    </div> 
</form:form> 

更多信息請參見http://docs.spring.io/spring-security/site/docs/current/reference/html/jc.html#jc-form

+0

是的,這個工程!我現在可以登錄並查看受保護的頁面。但是我不能註銷,所以我需要在那部分工作。下一步是添加JDBC登錄,但是你幫助我渡過了這個障礙。謝謝! – Eric

相關問題