1、SpringSecurity权限管理
登录后判断用户权限
<!--判断用户是否登录,如果登录查看登录者信息-->
<div sec:authorize="isAuthenticated()">
<p>
登录者:
<span sec:authentication="name"></span>
</p>
<p>
角色:
<span sec:authentication="principal.authorities"></span>
</p>
</div>
<!--判断角色的权限是否有ADMIN-->
<div sec:authorize = "hasAnyAuthority('ADMIN')">
<div>admin</div>
</div>
<!-- 判断角色的权限是否有VISITOR-->
<div sec:authorize = "hasAnyAuthority('VISITOR')">
<div>visitor</div>
</div>
2、开启某些页面的权限
在SecurityConfig类中添加授权放行,无须登录即可访问
//做拦截
@Override
protected void configure(HttpSecurity http) throws Exception {
// 请求授权
http.formLogin().and().authorizeRequests()
//授权放行
.antMatchers("/visitorLogin","/index","/mood","/findMood","/findAllBlog","/findAllAlbum","/findAllArchives","/link",
"/css/**","/images/**","/js/**","/layer/**","/socia1/**","/statics/**","/upload/**").permitAll()
//所有请求
.anyRequest()
//都需要身份认证
.authenticated().and()
//43、使用Layer打开select-mood子页面并配置SpringSecurity允许Iframe嵌入页面
.headers().frameOptions().disable().and()
//跨站请求伪造的防护
.csrf().disable()
//添加我们所写的spring social配置
.apply(zzzSocialSecurityConfig);
}
3、SpringSecurity实现不同身份的用户看到的页面不同
用以下代码包裹对非admin用户需要屏蔽的内容
<div sec:authorize = "hasAnyAuthority('ADMIN')">
admin才能看到的内容
</div>
4、QQ登录成功后转到主页
如果不能自动转到主页,添加下面代码:
@RequestMapping("/signin")
public String signin() {
return "redirect:/index";
}