在ShiroUserMapper.xml中新增内容

  <select id="getRolesByUserId" resultType="java.lang.String" parameterType="java.lang.Integer">
select r.roleid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role r
where u.userid = ur.userid and ur.roleid = r.roleid
and u.userid = #{userid}
</select> <select id="getPersByUserId" resultType="java.lang.String" parameterType="java.lang.Integer">
select p.permission from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp,t_shiro_permission p
where u.userid = ur.userid and ur.roleid = rp.roleid and rp.perid = p.perid
and u.userid = #{userid}
</select>

Service层

package com.jt.service;

import com.jt.model.ShiroUser;
import org.apache.ibatis.annotations.Param; import java.util.Set; public interface ShiroUserService { ShiroUser queryByName(@Param("uname") String uname); int insert(ShiroUser record); Set<String> getRolesByUserId(@Param("userid") Integer userid); Set<String> getPersByUserId(@Param("userid") Integer userid); }
ShiroUserServiceImpl
package com.jt.service.impl;

import com.jt.mapper.ShiroUserMapper;
import com.jt.model.ShiroUser;
import com.jt.service.ShiroUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.Set; @Service("shiroUserService")
public class ShiroUserServiceImpl implements ShiroUserService {
@Autowired
private ShiroUserMapper shiroUserMapper; @Override
public ShiroUser queryByName(String uname) {
return shiroUserMapper.queryByName(uname);
} @Override
public int insert(ShiroUser record) {
return shiroUserMapper.insert(record);
} @Override
public Set<String> getRolesByUserId(Integer userid) {
return shiroUserMapper.getRolesByUserId(userid);
} @Override
public Set<String> getPersByUserId(Integer userid) {
return shiroUserMapper.getPersByUserId(userid);
}
}

重写自定义realm中的授权方法

 /**
* 授权的方法
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String uname=principalCollection.getPrimaryPrincipal().toString();
ShiroUser shiroUser = this.shiroUserService.queryByName(uname);
Set<String> perids= this.shiroUserService.getPersByUserId(shiroUser.getUserid());
Set<String> roleIds= this.shiroUserService.getRolesByUserId(shiroUser.getUserid());
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
info.setRoles(roleIds);
info.setStringPermissions(perids);
return info;
}

注解式开发

常用注解介绍

@RequiresAuthenthentication:表示当前Subject已经通过login进行身份验证;即 Subject.isAuthenticated()返回 true

@RequiresUser:表示当前Subject已经身份验证或者通过记住我登录的

@RequiresGuest:表示当前Subject没有身份验证或者通过记住我登录过,即是游客身份

@RequiresRoles(value = {"admin","user"},logical = Logical.AND):表示当前Subject需要角色admin和user

@RequiresPermissions(value = {"user:delete","user:b"},logical = Logical.OR):表示当前Subject需要权限user:delete或者user:b

注解的使用

Controller层

 @RequiresUser
@ResponseBody
@RequestMapping("/passUser")
public String passUser(HttpServletRequest req) {
return ",身份认证成功,能够访问!!!";
} @RequiresRoles(value = {""},logical = Logical.AND)
@ResponseBody
@RequestMapping("/passRole")
public String passRole(HttpServletRequest req) {
return ",角色认证成功,能够访问!!!";
} @RequiresPermissions(value = {"user:update","user:load"},logical = Logical.AND)
@ResponseBody
@RequestMapping("/passPer")
public String passPer(HttpServletRequest req) {
return ",权限认证成功,能够访问!!!";
}

springmvc-servlet.xml

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"></property>
</bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">
unauthorized
</prop>
</props>
</property>
<property name="defaultErrorView" value="unauthorized"/>
</bean>

Jsp测试代码

<ul>
shiro注解
<li>
<a href="${pageContext.request.contextPath}/passUser">用户认证</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/passRole">角色</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/passPer">权限认证</a>
</li>
</ul>

预测结果

zs只能查看身份认证的按钮内容

ls、ww可以看权限认证按钮内容

zdm可以看所有按钮的内容

shiro授权、注解式开发的更多相关文章

  1. shiro授权+注解式开发

    shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...

  2. Shiro授权及注解式开发

    目的: shiro授权 shiro注解式开发 Shiro授权 首先设计shiro权限表: 从图中我们也清晰的看出五张表之间的关系 ShiroUserMapper Set<String> g ...

  3. 【转】Eclipse中设置ButterKnife进行注解式开发步骤 -- 不错

    原文网址:http://www.bubuko.com/infodetail-974262.html 最近在进行Android注解式开发的学习,正在尝试用ButterKnife.ButterKnife的 ...

  4. 总结切面编程AOP的注解式开发和XML式开发

    有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...

  5. SpringMVC 注解式开发

    SpringMVC的注解式开发是指,处理器是基于注解的类的开发.对于每一个定义的处理器,无需再配置文件中逐个注册,只需在代码中通过对类与方法的注解,便可完成注册.即注解替换是配置文件中对于处理器的注册 ...

  6. Spring MVC注解式开发

    MVC注解式开发即处理器基于注解的类开发, 对于每一个定义的处理器, 无需在xml中注册. 只需在代码中通过对类与方法的注解, 即可完成注册. 定义处理器 @Controller: 当前类为处理器 @ ...

  7. 《SpringMVC从入门到放肆》十一、SpringMVC注解式开发处理器方法返回值

    上两篇我们对处理器方法的参数进行了分别讲解,今天来学习处理器方法的返回值. 一.返回ModelAndView 若处理器方法处理完后,需要跳转到其它资源,且又要在跳转资源之间传递数据,此时处理器方法返回 ...

  8. 《SpringMVC从入门到放肆》九、SpringMVC注解式开发(简单参数接收)

    上一篇我们学习了注解式开发的配置方式并写了一个小Demo跑起来.今天我们来学习注解开发的参数接收.处理器方法中的常用参数有五类,这些参数会在系统调用时由系统自动赋值,即程序员可以在方法中直接使用.具体 ...

  9. 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)

    上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...

随机推荐

  1. Leetcode题解 - DFS部分简单题目代码+思路(113、114、116、117、1020、494、576、688)

    这次接触到记忆化DFS,不过还需要多加练习 113. 路径总和 II - (根到叶子结点相关信息记录) """ 思路: 本题 = 根到叶子结点的路径记录 + 根到叶子结点 ...

  2. python 调试大法

    说在前面 我觉得没有什么错误是调试器无法解决的,如果没有,那我再说一遍,如果有,那当我没说 一.抛出异常 可以通过 raise 语句抛出异常,使程序在我们已经知道的缺陷处停下,并进入到 except ...

  3. eCharts二三维地图总结

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 最近多个项目中的登录页面陆续提出了不少地图需求,主要围绕地图的 ...

  4. [转]VB.NET DataTable Select Function

    本文转自:https://www.dotnetperls.com/datatable-select-vbnet VB.NET DataTable Select Function This VB.NET ...

  5. synchronized凭什么锁得住?

    相关链接: <synchronized锁住的是谁?> 我们知道synchronized是重量级锁,我们知道synchronized锁住的是一个对象上的Monitor对象,我们也知道sync ...

  6. Redis速记

    参考: 集群 https://blog.csdn.net/xiaoxiaoyusheng2012/article/details/82051744 主从 https://blog.csdn.net/u ...

  7. 使用webstrom开发小程序要做的设置

    1.关闭rpx的错误提示 在setting里面  -->搜索inspections --> 在右侧找到invalid CSS property value    把对勾划掉

  8. Tensorflow常用算数操作

    TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU 还是 GPU, TensorFlow 能自动检测.如果检测 ...

  9. 数组累计-reduce

    reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. array.reduce(f ...

  10. ansible+playbook 搭建lnmp环境

    用三台机器 做ansible+playbook 搭建lnmp环境 IP分配 ansible 主机192.168.202.132 lnmp第一台主机 192.168.202.131 lnmp第一台主机 ...