目录
- <select id="selectRoleIdsByUserName" resultType="java.lang.String" parameterType="java.lang.String">
- select roleid from t_shiro_user u,t_shiro_user_role ur where u.userid = ur.userid and u.username = #{userName}
- select>
-
- <select id="selectPerIdsByUserName" resultType="java.lang.String" parameterType="java.lang.String">
- select rp.perid from t_shiro_user u,t_shiro_user_role ur,t_shiro_role_permission rp where u.userid = ur.userid and ur.roleid = rp.roleid and u.username = #{userName}
- select>
- package com.cdl.ssm.mapper;
-
- import com.cdl.ssm.model.User;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.stereotype.Repository;
-
- import java.util.Set;
-
- @Repository
- public interface UserMapper {
- int deleteByPrimaryKey(Integer userid);
-
- int insert(User record);
-
- int insertSelective(User record);
-
- User selectByPrimaryKey(Integer userid);
-
- /*通过账号插询*/
- User queryUserByUserName(@Param("userName") String userName);
-
- //通过用户名拿到角色ID
- Set
selectRoleIdsByUserName(@Param("userName") String userName); -
- //通过用户名拿到权限ID
- Set
selectPerIdsByUserName(@Param("userName") String userName); -
- int updateByPrimaryKeySelective(User record);
-
- int updateByPrimaryKey(User record);
- }
- package com.cdl.ssm.biz;
-
- import com.cdl.ssm.model.User;
-
- import java.util.Set;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-25 18:48
- */
- public interface UserBiz {
-
- int deleteByPrimaryKey(Integer userid);
-
- int insert(User record);
-
- int insertSelective(User record);
-
- User selectByPrimaryKey(Integer userid);
-
- /*通过账号插询*/
- User queryUserByUserName( String userName);
-
- //通过用户名拿到角色ID
- Set
selectRoleIdsByUserName(String userName); -
- //通过用户名拿到权限ID
- Set
selectPerIdsByUserName(String userName); -
- int updateByPrimaryKeySelective(User record);
-
- int updateByPrimaryKey(User record);
-
- }
- package com.cdl.ssm.biz.impl;
-
- import com.cdl.ssm.biz.UserBiz;
- import com.cdl.ssm.mapper.UserMapper;
- import com.cdl.ssm.model.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.Set;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-25 18:49
- */
- @Service("userBiz")
- public class UserBizImpl implements UserBiz {
- @Autowired
- private UserMapper userMapper;
-
- @Override
- public int deleteByPrimaryKey(Integer userid) {
- return userMapper.deleteByPrimaryKey(userid);
- }
-
- @Override
- public int insert(User record) {
- return userMapper.insert(record);
- }
-
- @Override
- public int insertSelective(User record) {
- return userMapper.insertSelective(record);
- }
-
- @Override
- public User selectByPrimaryKey(Integer userid) {
- return userMapper.selectByPrimaryKey(userid);
- }
-
- @Override
- public User queryUserByUserName(String userName) {
- return userMapper.queryUserByUserName(userName);
- }
-
- @Override
- public Set
selectRoleIdsByUserName(String userName) { - return userMapper.selectRoleIdsByUserName(userName);
- }
-
- @Override
- public Set
selectPerIdsByUserName(String userName) { - return userMapper.selectPerIdsByUserName(userName);
- }
-
- @Override
- public int updateByPrimaryKeySelective(User record) {
- return userMapper.updateByPrimaryKeySelective(record);
- }
-
- @Override
- public int updateByPrimaryKey(User record) {
- return userMapper.updateByPrimaryKey(record);
- }
- }
-
- package com.cdl.ssm.shiro;
-
- import com.cdl.ssm.biz.UserBiz;
- import com.cdl.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 cdl
- * @site www.cdl.com
- * @create 2022-08-25 19:08
- */
- public class MyRealm extends AuthorizingRealm {
-
- public UserBiz userBiz;
-
- public UserBiz getUserBiz() {
- return userBiz;
- }
-
- public void setUserBiz(UserBiz userBiz) {
- this.userBiz = userBiz;
- }
-
-
-
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
- System.out.println("用户授权...");
- String userName = principals.getPrimaryPrincipal().toString();//获取账户名
- Set
roleIds = userBiz.selectRoleIdsByUserName(userName); - Set
perIds = userBiz.selectPerIdsByUserName(userName); - SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
- //将当前登录的权限 交给shiro的授权器
- info.setStringPermissions(perIds);
- //将当前角色的权限 交给shiro的授权器
- info.setRoles(roleIds);
- return info;
- }
-
- @Override
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
- String userName = token.getPrincipal().toString();
- User user = userBiz.queryUserByUserName(userName);
- AuthenticationInfo info = new SimpleAuthenticationInfo(
- user.getUsername(),
- user.getPassword(),
- ByteSource.Util.bytes(user.getSalt()),
- this.getName()//realm的名字
- );
- return info;
- }
- }
-
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
-
-
- <bean id="shiroRealm" class="com.cdl.ssm.shiro.MyRealm">
- <property name="userBiz" ref="userBiz" />
-
-
-
-
- <property name="credentialsMatcher">
- <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
-
- <property name="hashAlgorithmName" value="md5"/>
-
- <property name="hashIterations" value="1024"/>
-
- <property name="storedCredentialsHexEncoded" value="true"/>
- bean>
- property>
- bean>
-
-
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="shiroRealm" />
- bean>
-
-
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
-
- <property name="securityManager" ref="securityManager" />
-
- <property name="loginUrl" value="/login"/>
-
-
-
- <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
-
- <property name="filterChainDefinitions">
- <value>
-
-
-
-
- /user/login=anon
- /user/updatePwd.jsp=authc
- /admin/*.jsp=roles[4]
- /user/teacher.jsp=perms[2]
-
- value>
- property>
- bean>
-
-
- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
- beans>
运行登录界面 输入zs的正确密码和账号之后
输入zdm的 有用户新增的权限
@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
- <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>
-
- package com.cdl.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;
-
- import javax.servlet.http.HttpServletRequest;
-
- /**
- * @author cdl
- * @site www.cdl.com
- * @create 2022-08-26 19:54
- */
- @RequestMapping("/shiro")
- @Controller
- public class ShiroController {
-
- @RequiresUser//代表只有通过登录后才能通过
- @RequestMapping("/passUser")
- public String passUser(HttpServletRequest request){
- System.out.println("身份认证通过、、、");
- return "admin/addUser";
- }
-
- @RequiresRoles(value = {"1","4"},logical = Logical.AND)
- @RequestMapping("/passRole")
- public String passRole(HttpServletRequest request){
- System.out.println("角色认证通过、、、");
- return "admin/listUser";
- }
-
- @RequiresPermissions(value = {"user:update","user:view"},logical = Logical.OR)
- @RequestMapping("/passPer")
- public String passPer(HttpServletRequest request){
- return "admin/resetPwd";
- }
-
- @RequestMapping("/unauthorized")
- public String unauthorized(){
- return "unauthorized";
- }
-
-
- }
输入zs的账号和密码