Spring Boot 整合Spring Security

HttpSecurity(接口权限管理)

protected void configure(HttpSecurity http) throws Exception {
	http.formLogin()//使用表单登录
	.loginPage("login.html")//自定义请求页面
	.permitAll()//声明该界面的权限(permitAll:任何人都可以访问)
	.loginProcessingUrl("/web/user/login")//默认登录接口
	.usernameParameter("username")//账号
	.passwordParameter("password")//密码
	.successHandler(new SuccessHandler())//登录成功处理方式
	.failureHandler(new FailureHandler())//登录失败处理方式
	.successForwardUrl("/index")//登录成功跳转页面
	.defaultSuccessUrl("/index.html",true)//登录成功后跳转指定页面
	.and()
	.anthorizeRequests()
	.anyRequest().authenticated()
	.and().logout().logoutUrl()
	.logoutSuccessHandler(new ExitSuccessHandler())//退出登录成功后执行方式
	.clearAuthentication(true)//清除身份认证信息
	.invalidateHttpSession(true)//使session失效
	.addLogoutHandler(new ExitHandler())
	.deleteCookies("JSESSIONID","token")//退出后删除指定cookie信息
	.and()
	.sessionManagement()
	.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)//反之session固定攻击
	.maximumSessions(1)//只允许N台设备同时在线(1台),需要重写用户实体类的hashCode()
	.maxSessionsPreventsLogin(false)//false:允许后登陆的设备挤掉前面的设备;true:设备达到上限以后,不允许后面的设备登录
	.expiredUrl("/login")//session失效返回路径
	.expiredSessionStrategy(customExpiredSessionStrategy)
	;
}

表单管理(formLogin)

方法名参数类型描述
loginPage()String之定义默认跳转页面
permitAll()/所有人都能访问
loginProcessingUrlString自定义登录请求接口
usernameParameterString执行身份验证时查找用户名的HTTP参数
passwordParameterString执行身份验证时查找密码的HTTP参数
successHandlerAuthenticationSuccessHandler登录成功处理方式
failureHandlerAuthenticationFailureHandler登录失败处理方式
successForwardUrlString转发身份验证成功处理程序
defaultSuccessUrlString如果用户在验证之前没有访问过安全页面,则在验证成功后用户将被重定向到何处。这是一个捷径

认证请求(authorizeRequests)

方法名参数类型描述
antMatchersString…指定接口赋予权限
permitAll/所有权限
hasRoleString指定权限
accessString指定权限

会话管理

方法名参数类型描述
sessionCreationPolicySessionCreationPolicy防止session固定攻击
maximumSessionsint限制同时在线人数
maxSessionsPreventsLoginboolean设备达到上限后,是否允许挤掉之前登录的设备:true:不允许;false:允许
expiredUrlStringsession失效返回路径
expiredSessionStrategyCustomExpiredSessionStrategysession失效处理
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)//防止Session的固定攻击的

可以通过以下选项准确控制会话何时创建以及Spring Security如何与之交互:

机制描述
ALWAYS如果没有session存在就创建一个
NEVERSpringSecurity 将不会创建Session,但是如果应用中其他地方创建了Session,那么Spring Security将会使用它。
IF_REQUIRED如果需要就创建一个Session(默认)登录时
STATELESSSpringSecurity将绝对不会创建Session,也不使用Session

退出登录(logout)

方法名参数类型描述
logoutUrlString配置退出登录路径
clearAuthenticationboolean是否清楚身份认证信息;true:是;false:否
invalidateHttpSessionboolean是否让session失效;true:是;false:否
deleteCookiesString…退出登录后清楚指定cookie信息

AuthenticationManagerBuilder(身份认证管理)

/**
	 * 身份认证管理
	 */
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// TODO Auto-generated method stub
		auth.userDetailsService(userSecurityService).passwordEncoder(passwordEncoder());
		//super.configure(auth);
	}

WebSecurity(静态资源管理)

/**
	 * 静态资源管理
	 */
	@Override
	public void configure(WebSecurity web) throws Exception {
		// TODO Auto-generated method stub
		//web.ignoring().antMatchers("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.ico","/**/*.woff","/**/*.eot","/**/*.svg","/**/*.ttf");
		web.ignoring().antMatchers("/images/**","/static/**", "/css/**", "/fonts/**", "/img/**", "/js/**");
		//super.configure(web);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值