Spring security 权限控制

方法级别权限控制

1.jsr250

   1.需要坐标 

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>jsr250-api</artifactId>
            <version>1.0</version>
        </dependency>

 2.在配置文件spring-security.xml 中开启 注解  默认是关闭的

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

3.在控制器中 添加注解 @RolesAllowed("ADMIN")   可以写多个角色       可以省略       只有 有admin角色的用户才能访问

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/findAll.do")
    @RolesAllowed("ADMIN")                只有 有admin角色的用户才能访问
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<UserInfo> list=iUserService.findAll();
        mv.addObject("userList",list);
        mv.setViewName("user-list");
        return mv;
    }
}

@PermitAll    表示允许所有的角色进行访问,也就是说不进行权限控制
@DenyAll      是和PermitAll相反的,表示无论什么角色都不能访问

  4.权限不足时 前端会报 403 权限不足的错误 可以在web.xml配置 错误页面      将页面放在webapp下

  <error-page>
    <error-code>403</error-code>
    <location>/403.jsp</location>
  </error-page>

2.Secured注解

  是Spring 自带的注解 不需要再导入包了

 在spring-security.xml配置文件 开启注解 功能

 <security:global-method-security secured-annotations="enabled"/>
     @RequestMapping("/findAll.do")
//    @RolesAllowed("ADMIN")
    @Secured("ROLE_ADMIN")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<UserInfo> list=iUserService.findAll();
        mv.addObject("userList",list);
        mv.setViewName("user-list");
        return mv;
    }

 与jsr250区别是   必须写配置文件中声明的  拦截角色名称   

<security:http auto-config="true" use-expressions="false">
        <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
        <security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>

        <!-- 定义跳转的具体的页面 -->
        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login.do"
                default-target-url="/index.jsp"
                authentication-failure-url="/failer.jsp"
                authentication-success-forward-url="/pages/main.jsp"
        />

        <!-- 关闭跨域请求 -->
        <security:csrf disabled="true"/>
        <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />

    </security:http>

Secured注解 支持spl表达式

  需要在  spring-security.xml 开启功能

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

   "hasRole('ROLE_ADMIN')"     只有ROLE_ADMIN 角色的用户 才能查询所有

 @RequestMapping("/findAll.do")
//    @RolesAllowed("ADMIN")
//    @Secured("ROLE_ADMIN")
    @PreAuthorize("hasRole('ROLE_ADMIN')")            
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<UserInfo> list=iUserService.findAll();
        mv.addObject("userList",list);
        mv.setViewName("user-list");
        return mv;
    }

  "authentication.principal.username == 'wyc'"    只有用户名为wyc 的用户才能使用save方法

    @RequestMapping("/save.do")
    @PreAuthorize("authentication.principal.username == 'wyc'")
    public String saveUser(UserInfo userInfo) {
        iUserService.save(userInfo);
        return "redirect:findAll.do";
    }

表达式

描述

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。

3.页面端标签 控制权限

   在jsp页面中我们可以使用spring security提供的权限标签来进行权限控制      导入坐标

<dependency> 
  <groupId>org.springframework.security</groupId> 
  <artifactId>spring-security-taglibs</artifactId> 
  <version>version</version> 
</dependency>

  jsp 页面 需要导入坐标 

<%@taglib uri="http://www.springframework.org/security/tags" prefix="security"%>

  在spring-security.xml 配置文件   开启支持 jsp页面的 SpringEL表达式 功能     所以以前的 改为 EL表达式的形式

<security:http auto-config="true" use-expressions="true">      开启spring EL 表达式
        <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>

        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login.do"
                default-target-url="/index.jsp"
                authentication-failure-url="/failer.jsp"
                authentication-success-forward-url="/pages/main.jsp"
        />
        <security:csrf disabled="true"/>
        <security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp" />
    </security:http>

1. authentication    在jsp页面 获取当前登录用户     principal.username 获取当前用户的 用户名 显示在线

<div class="pull-left info">
    <p><security:authentication property="principal.username"/></p>
    <a href="#"><i class="fa fa-circle text-success"></i> 在线</a>
</div>

2.  authorize    控制 页面 显示标签的权限

   只有admin  角色的用户 才能  显示 被security:authorize 包裹的 标签

 <security:authorize access="hasRole('ADMIN')">
    <li id="system-setting">
        <a href="${pageContext.request.contextPath}/sysLog/findAll.do">
             <i class="fa fa-circle-o"></i> 访问日志
        </a>
    </li>
 </security:authorize>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值