java
  • jsp
  • spring-mvc
  • spring-security
  • spring-boot
  • 2015-02-23 58 views 4 likes 
    4

    我的問題是:當我嘗試在我的自定義登錄頁面上登錄時,它將我重新導向登錄頁面(這不要緊,如果我把正確或錯誤的憑據)。在調試中我沒有達到自定義的UserDetailService也很奇怪,我認爲這意味着Spring甚至不檢查用戶的憑證。無法登錄到我的自定義登錄頁面在春季啓動安全

    我有自定義登錄表單/WEB-INF/jsp/login.jsp

    ...  
    <form name='loginForm' action="<c:url value='j_spring_security_check' />" method='POST'> 
    
         <table> 
          <tr> 
           <td>User:</td> 
           <td><input type='text' name='j_username' value=''></td> 
          </tr> 
          <tr> 
           <td>Password:</td> 
           <td><input type='password' name='j_password' /></td> 
          </tr> 
          <tr> 
           <td colspan='2'><input name="submit" type="submit" 
                 value="submit" /></td> 
          </tr> 
         </table> 
    
         <input type="hidden" name="${_csrf.parameterName}" 
           value="${_csrf.token}" /> 
    
        </form> 
    ... 
    

    這是配置:

    @Configuration 
    @EnableWebMvcSecurity 
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    
        @Autowired 
        private UserDetailsService userDetailsService; 
    
        @Override 
        protected void configure(HttpSecurity http) throws Exception { 
         http 
           .authorizeRequests().antMatchers("/", "/welcome", "/resources/**").permitAll() 
           .anyRequest().authenticated().and() 
           .formLogin().loginPage("/login").failureUrl("/login?error").permitAll().and() 
           .logout().logoutUrl("/login?logout").permitAll(); 
        } 
    
        @Autowired 
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
         auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 
        } 
    
        @Bean 
        public PasswordEncoder passwordEncoder() { 
         return new BCryptPasswordEncoder(); 
        } 
    } 
    

    這是內部控制:

    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public ModelAndView login(
         @RequestParam(value = "error", required = false) String error, 
         @RequestParam(value = "logout", required = false) String logout) { 
    
        ModelAndView model = new ModelAndView(); 
        if (error != null) { 
         model.addObject("error", "Invalid username and password!"); 
        } 
    
        if (logout != null) { 
         model.addObject("msg", "You've been logged out successfully."); 
        } 
        model.setViewName("login"); 
    
        return model; 
    
    } 
    

    src/main/resources/application.properties我:

    spring.view.prefix: /WEB-INF/jsp/ 
    spring.view.suffix: .jsp 
    

    回答

    8

    那些j_spring_security_checkj_username,j_password名稱都是舊的默認值(Spring Security 3.2之前的版本)。

    現在的默認值是:login,usernamepassword

    相關問題