spring security-方法权限控制

spring security-方法权限控制基本使用

在服务端中spring security可以使用注解的方式去控制用户访问方法,常见的注解方式有三种:JSR-250注解、@Secured注解、spring El表达式注解方式,三种注解方式默认都是关闭状态,可以在spring-security.xml文件中统一开启

1
<security:global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"></security:global-method-security>

黑马的笔记中有这么一段话:Spring Security默认是禁用注解的,要想开启注解,需要在继承
WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解,并在该类中将
AuthenticationManager定义为Bean。个人不清楚这句话的实际操作,但在测试中没有执行该操作也能正常实现方法权限控制,下面时三种注解方式的基本使用

JSR-250

1、引入依赖

1
2
3
4
5
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>

2、在spring-security配置文件中开启注解

1
<security:global-method-security jsr250-annotations="enabled"/>

3、在指定方法中添加注解权限

@RolesAllowed表示访问该方法应该具有的角色

@PermitAll该方法所有角色都能访问

@DenyAll该方法所有角色都不能访问

1
2
3
4
5
6
7
8
9
10
11
12
@RolesAllowed({"ADMIN"})
@RequestMapping("/findAll")
public ModelAndView findAll(){
ModelAndView mav = new ModelAndView();
try {
mav.addObject("productList",productService.findAll());
} catch (Exception e) {
e.printStackTrace();
}
mav.setViewName("product-list");
return mav;
}

@Secured注解

1、在spring-security配置文件中开启注解**

1
<security:global-method-security secured-annotations="enabled"/>

2、在指定方法中添加注释权限

1
2
3
4
5
6
7
8
9
10
11
12
@Secured({"ROLE_ADMIN"})
@RequestMapping("/findAll")
public ModelAndView findAll(){
ModelAndView mav = new ModelAndView();
try {
mav.addObject("productList",productService.findAll());
} catch (Exception e) {
e.printStackTrace();
}
mav.setViewName("product-list");
return mav;
}

spring EL表达式注释

服务端的使用

1、在spring-security配置文件中开启注解**

1
<security:global-method-security pre-post-annotations="disabled"/>

2、在指定方法中添加注释权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//方式一:指定特定的用户
@RequestMapping("/findAll")
@PreAuthorize("authentication.principal.username == 'user'")
public ModelAndView findAll(){
ModelAndView mav = new ModelAndView();
try {
mav.addObject("productList",productService.findAll());
} catch (Exception e) {
e.printStackTrace();
}
mav.setViewName("product-list");
return mav;
}

//方式二:指定用户角色
@RequestMapping("/findAll")
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
public ModelAndView findAll(){
ModelAndView mav = new ModelAndView();
try {
mav.addObject("productList",productService.findAll());
} catch (Exception e) {
e.printStackTrace();
}
mav.setViewName("product-list");
return mav;
}

403页面配置

对于没有访问权限的角色,访问方法时会报403错误,此时可以配置一个自定义的403错误页面,只需在web.xml文件加入以下配置

1
2
3
4
5
//下面配置表示,出现403错误代码时,重定向到自定义的页面
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
在页面端的使用

1、引入依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>

2、在页面中使用y

1
2
3
4
5
6
7
8
9
10
//1、先在jsp页面中导入
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

//在页面中获取用户名
<security:authentication property="principal.username"></security:authentication>

//根据权限显示菜单,只有匹配ROLE_ADMIN角色才显示xxx内容
<security:authorize access="hasAnyRole('ROLE_ADMIN')">
xxx
</security:authorize>
@PreAuthorize注解的其他使用

Spring Security允许我们在定义URL访问或方法访问所应有的权限时使用Spring EL表达式,在定义所需的访问权限时如果对应的表达式返回结果为true则表示拥有对应的权限,反之则无。Spring Security可用表达式对象的基类是SecurityExpressionRoot,其为我们提供了如下在使用Spring EL表达式对URL或方法进行权限控制时通用的内置表达式

表达式 描述
hasRole([role]) 当前用户是否拥有指定角色。
hasAnyRole([role1,role2]) 多个角色是一个以逗号进行分隔的字符串。如果当前用户拥有指定角色中的任意一个则返回true。
hasAuthority([auth]) 等同于hasRole
hasAnyAuthority([auth1,auth2]) 等同于hasAnyRole
Principle 代表当前用户的principle对象
authentication 直接从SecurityContext获取的当前Authentication对象
permitAll 总是返回true,表示允许所有的
denyAll 总是返回false,表示拒绝所有的
isAnonymous() 当前用户是否是一个匿名用户
isRememberMe() 表示当前用户是否是通过Remember-Me自动登录的
isAuthenticated() 表示当前用户是否已经登录认证成功了。
isFullyAuthenticated() 如果当前用户既不是一个匿名用户,同时又不是通过Remember-Me自动登录的,则返回true。
打赏