目录
1.shiro授权角色、权限

在ShiroUserMapper.xml中新增内容
<select id="selectRoleIdsByUserName" 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="selectPerIdsByUserName" 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>
userMapper.java
public Set<String> selectRoleIdsByUserName(Integer userId);
public Set<String> selectPerIdsByUserName(Integer userId);
userbizimpl.java
package com.hmj.ssm.Biz.impl;
import com.hmj.ssm.Biz.UserBiz;
import com.hmj.ssm.mapper.UserMapper;
import com.hmj.ssm.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;
/**
* @author 小何吖
* @create 2022-08-25 18:30
*/
@Service("userBiz")
public class UserBizImpl implements UserBiz {
@Autowired
private UserMapper userMapper;
@Override
public Set<String> selectRoleIdsByUserName(String userName) {
return userMapper.selectRoleIdsByUserName(userName);
}
@Override
public Set<String> selectPerIdsByUserName(String userName) {
return userMapper.selectPerIdsByUserName(userName);
}
}
MyRealm.java
package com.hmj.ssm.shiro;
import com.hmj.ssm.Biz.UserBiz;
import com.hmj.ssm.model.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import java.util.Set;
/**
* @author 小何吖
* @create 2022-08-25 18:33
*/
public class MyRealm extends AuthorizingRealm {
public UserBiz userBiz;
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
/**
* 授权
* @param principalCollection
* @return
* shiro-web.ini
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
System.out.println("用户授权...");
String username = principals.getPrimaryPrincipal().toString();
ShiroUser user = shiroUserService.queryByName(username);
Set<String> roles = shiroUserService.getRolesByUserId(user.getUserid());
Set<String> pers = shiroUserService.getPersByUserId(user.getUserid());
// SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// info.addRoles(roles);
// info.addStringPermissions(pers);
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
info.setRoles(roles);
info.setStringPermissions(pers);
return info;
}
}
2.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
Controller层
ShiroController
package com.hmj.ssm.controller;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 小何吖
* @create 2022-08-26 20:03
*/
@Controller
@RequestMapping("/shiro")
public class ShiroController {
// RequiresUser代表,当前方法只有登录后才能访问
// RequiresUser 等价于 spring-shiro.xml中的user/updatePwd.jsp配置
@RequiresUser
@RequestMapping("/passUser")
public String passUser(){
System.out.println("身份认证通过");
return "admin/addUser";
}
// RequiresRoles 代表 当前方法只有 具备指定的角色 才能够访问
// RequiresUser 等价于 spring-shiro.xml中的user/updatePwd.jsp配置
@RequiresRoles(value = {"1","4"},logical = Logical.AND)
@RequestMapping("/passRole")
public String passRole(){
System.out.println("角色认证通过");
return "admin/addUser";
}
// RequiresPermissions 代表 当前方法只有 具备指定的角色 才能够访问
// RequiresPermissions 等价于 spring-shiro.xml中的user/teacher.jsp=perms[2]配置
@RequiresPermissions(value = {"2"},logical = Logical.AND)
@RequestMapping("/passPermission")
public String passPermission(){
System.out.println("权限认证通过");
return "admin/addUser";
}
}
Springmvc.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>