spring security 參數配置
- // 自定義登錄頁面
- http.csrf().disable().formLogin().loginPage("/login") //指定登錄頁的路徑
- .failureUrl("/login?error=1")
- .loginProcessingUrl("/j_spring_security_check")
- .usernameParameter("j_username")
- .passwordParameter("j_password").permitAll();
- // 自定義注銷
- http.logout().logoutUrl("/logout").logoutSuccessUrl("/login")
- .invalidateHttpSession(true);
- // session管理
- http.sessionManagement().sessionFixation().changeSessionId()
- .maximumSessions(1).expiredUrl("/");
- // RemeberMe
- http.rememberMe().key("webmvc#FD637E6D9C0F1A5A67082AF56CE32485");
// 自定義登錄頁面
.and()
.formLogin().loginPage("/jsp/login.jsp")
.failureUrl("/jsp/login.jsp?error=1")
.loginProcessingUrl("/spring_security_check")
.usernameParameter("username")
.passwordParameter("password").permitAll().defaultSuccessUrl("/index.do"); //如果開啟了CSRF 退出則需要使用POST訪問,可以使用以下方式解決,但并不推薦
http.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
// 登陸成功后跳轉的地址,以及刪除的cookie名稱
.logoutSuccessUrl("/jsp/login.jsp?error=logout") .invalidateHttpSession(true);
- .and()
- .formLogin()
- .loginPage("/login")//指定登錄頁是”/login”
- .permitAll()
- .successHandler(loginSuccessHandler()) //登錄成功后可使用loginSuccessHandler()存儲用戶信息,可選。
public class LoginSuccessHandler extends- SavedRequestAwareAuthenticationSuccessHandler {
- @Override
- public void onAuthenticationSuccess(HttpServletRequest request,
- HttpServletResponse response, Authentication authentication) throws IOException,
- ServletException {
- //獲得授權后可得到用戶信息 可使用SUserService進行數據庫操作
- SysUser userDetails = (SysUser)authentication.getPrincipal();
- //輸出登錄提示信息
- System.out.println("管理員 " + userDetails.getName() + " 登錄");
- super.onAuthenticationSuccess(request, response, authentication);
- } }
posted on 2018-03-19 20:33 風人園 閱讀(437) 評論(0) 編輯 收藏 所屬分類: SpringSecurity