shiro的授权角色、权限
在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>
在ShiroUserMapper.java中用set集合进行接收xml中传过来的方法
因为set集合不会存储重复的元素
/**
* 查询用户对应的角色id集合
* @param userid
* @return
*/
Set<String> getRolesByUserId(@Param("userid") Integer userid);
/**
* 查询用户对应的权限名称集合
* @param userid
* @return
*/
Set<String> getPersByUserId(@Param("userid") Integer userid);
在ShiroUserService.java中添加内容,并实现对应接口中的内容
ShiroUserService.java
package com.ljy.ssm.service;
import com.ljy.ssm.model.ShiroUser;
import java.util.Set;
/**
* @author ljy
* @site www.fellingss.com
* @company
* @create 2019-12-02 0:03
*/
public interface ShiroUserService {
//shiro授权
public Set<String> getRolesByUserId(Integer userId);
public Set<String> getPersByUserId(Integer userId);
ShiroUser queryByName(String userName);
int insert(ShiroUser record);
}
ShiroUserServiceImpl
@Override
public Set<String> getRolesByUserId(Integer userid) {
return shiroUserMapper.getRolesByUserId(userid);
}
@Override
public Set<String> getPersByUserId(Integer userid) {
return shiroUserMapper.getPersByUserId(userid);
}
在之前写过的myrealm中添加授权的方法
/**
* 授权
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("进行授权...");
// //角色以及权限查询
// 当前登录的用户
ShiroUser shiroUser = this.shiroUserService.queryByName(principalCollection.getPrimaryPrincipal().toString());
// 当前认证过的用户对应的角色id集合
Set<String> rolesByUserId = this.shiroUserService.getRolesByUserId(shiroUser.getUserid());
// 当前认证过的用户对应的权限id集合
Set<String> persByUserId = this.shiroUserService.getPersByUserId(shiroUser.getUserid());
AuthorizationInfo info = new SimpleAuthorizationInfo();
((SimpleAuthorizationInfo) info).setRoles(rolesByUserId);
((SimpleAuthorizationInfo) info).setStringPermissions(persByUserId);
return info;
}
shiro授权最大的优点是安全性非常高,同时也是缺点,性能会降低,因为每发一个请求都会去做一次权限认证
注解式开发
常用注解介绍
- @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
Cotroller层
HelloController.java
@ResponseBody
@RequestMapping("/passUser")
public String passUser(HttpServletRequest req){
return "pass user...";
}
@RequiresRoles(value = {"2","4"},logical= Logical.OR)
@ResponseBody
@RequestMapping("/passRole")
public String passRole(HttpServletRequest req){
return "pass role...";
}
@RequiresPermissions(value = {"user:load","user:export"},logical = Logical.AND)
@ResponseBody
@RequestMapping("/passAuth")
public String passAuth(HttpServletRequest req){
return "pass auth...";
}
拦截器
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>
只要是用户都可以访问
用zdm登录就全可以访问