⒈表單添加html
1 <form action="/authentication/form" method="post"> 2 <table> 3 <tr> 4 <td>用戶名:</td> 5 <td><input id="username" type="text" name="username"></td> 6 </tr> 7 <tr> 8 <td>密碼:</td> 9 <td><input id="password" type="password" name="password"></td> 10 </tr> 11 <tr> 12 <td>圖形驗證碼:</td> 13 <td> 14 <input type="text" name="imageCode"> 15 <img src="/code/image"> 16 </td> 17 </tr> 18 <tr> 19 <td colspan="2"><input name="remember-me" type="checkbox" value="true"/>記住我</td> 20 </tr> 21 <tr> 22 <td colspan="2"><button type="submit">登陸</button></td> 23 </tr> 24 </table> 25 </form>
⒉數據庫
1 @Autowired 2 private UserDetailsService userDetailsService; 3 4 @Bean 5 private DataSource dataSource; 6 7 @Bean 8 public PersistentTokenRepository persistentTokenRepository(){ 9 JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl(); 10 tokenRepository.setDataSource(dataSource); 11 tokenRepository.setCreateTableOnStartup(true); //系統在啓動的時候生成「記住我」的數據表(只能使用一次) 12 return tokenRepository; 13 } 14 @Override 15 protected void configure(HttpSecurity http) throws Exception { 16 ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter(); 17 validateCodeFilter.setAuthenticationFailureHandler(coreqiAuthenticationFailureHandler); 18 19 //http.httpBasic() //httpBasic登陸 BasicAuthenticationFilter 20 http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) //加載用戶名密碼過濾器的前面 21 .formLogin() //表單登陸 UsernamePasswordAuthenticationFilter 22 .loginPage("/coreqi-signIn.html") //指定登陸頁面 23 //.loginPage("/authentication/require") 24 .loginProcessingUrl("/authentication/form") //指定表單提交的地址用於替換UsernamePasswordAuthenticationFilter默認的提交地址 25 .successHandler(coreqiAuthenticationSuccessHandler) //登陸成功之後要用咱們自定義的登陸成功處理器,不用Spring默認的。 26 .failureHandler(coreqiAuthenticationFailureHandler) //本身體會把 27 .and() 28 .rememberMe() //對記住我進行設置 29 .tokenRepository(persistentTokenRepository()) 30 .tokenValiditySeconds(1000) //設置Token的有效時間 31 .userDetailsService(userDetailsService) //使用userDetailsService用Token從數據庫中獲取用戶自動登陸 32 .and() 33 .authorizeRequests() //對受權請求進行配置 34 .antMatchers("/coreqi-signIn.html","/code/image").permitAll() //指定登陸頁面不須要身份認證 35 .anyRequest().authenticated() //任何請求都須要身份認證 36 .and().csrf().disable(); //禁用CSRF 37 //FilterSecurityInterceptor 整個SpringSecurity過濾器鏈的最後一環 38 }