访问权限控制
粗粒度:对一个功能的访问进行控制
细粒度:对该功能下的数据显示进行控制
注意:权限控制,需要在spring-mvc.xml中配置,否则会导致失效
< aop: aspectj-autoproxy proxy-target-class = " true" > </ aop: aspectj-autoproxy>
jsr-250权限控制的使用:
第一步:导入依赖
< dependency>
< groupId> javax.annotation</ groupId>
< artifactId> jsr250-api</ artifactId>
< version> 1.0</ version>
</ dependency>
第二步:编写spring-security.xml,开启jsr-250注解的使用
< security: global-method-security jsr250-annotations = " enabled" />
第三步:在需要权限控制的类或方法上使用@RolesAllowed("ADMIN"),来控制访问所需要的角色,可以省略"ROLE_",开启表达式的使用, @RolesAlloewd("ADMIN")依然这么写
secured权限控制的使用:
第一步:编写spring-security.xml,开启secured注解的使用
< security: global-mathod-security secured-annotations = " enable" />
第三步:在需要权限控制的类或方法上使用@Secured("ROLE_ADMIN"),来控制访问所需要的角色,不能省略"ROLE_",
开启表达式的使用, @Secured("ROLE_ADMIN")依然这么写
SPEL表达式权限控制的使用:
在spring-security.xml中开启使用SPEL表达式,
< security: http auto-config = " true" use-expressions = " true" > </ security: http>
开启SPEL表达式后,需要修改:
< security: intercept pattern = " /**" access = " hasRole(' ROLE_ADMIN' ,' ROLE_USER' )" >
< security: global-method-security pre-post-annotations = " enabled" > </ security: global-method-security>
@PreAuthorize("authentication.principal.username == 'tom'") //当前用户为tom才可以访问
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')") //admin或者user角色都可以访问
@PreAuthorize("hasRole('ROLE_ADMIN')") //admin角色可以访问
第一步:导入依赖
< dependency>
< groupId> org.springframework.security</ groupId>
< artifactId> spring-security-taglibs</ artifactId>
< version> 5.0.1.RELEASE</ version>
</ dependency>
第二步:在JSP页面引入标签库
<%@taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
1.获取当前认证用户的信息
< h1> 用户名:< security: authentication property = " principal.username" /> </ h1>
2.控制菜单栏是否显示
< security: authorize access = " hasRole(' ROLE_ADMIN' )" >
....(菜单栏)
</ security: authorize>
@Param注解
由于#{}中赋值时,如果需要赋值的数据类型是普通数据类型,那么#{}可以任意写
@Insert("insert into user values(#{username},#{password})")
public void save(@Param("username"))String username,@Param("password")String password){ ... }
此时,由于赋值不明确,会导致500,可以使用@Param注解解决参数注入问题
手动指定错误状态码的跳转页面
配置wem.xml
< error-page>
< error-code> 403</ error-code>
< location> /403.jsp</ location>
</ error-page>
400:通常发生在spring自动封装前端数据异常,一般都是日期转换异常
403:通常是在使用了spring security框架后,发生的权限不足的问题