博客信息

Shiro授权及注解式开发

发布时间:『 2019-02-13 04:02』  博客类别:安全框架  阅读(1060)


小李飞刀_shiro



授权

ShiroUserMapper.xml中新增内容

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 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}

小李飞刀_shiro


Service

package com.javaxl.ssm.service;

import com.javaxl.ssm.model.ShiroUser;

import java.util.Set;

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 * @company
 * @create  2019-10-12 15:55
 */
public interface ShiroUserService {
    public Set<String> getRolesByUserId(Integer userId);

    public Set<String> getPersByUserId(Integer userId);

    public ShiroUser queryByName(String userName);
}


package com.javaxl.ssm.service.impl;

import com.javaxl.ssm.mapper.ShiroUserMapper;
import com.javaxl.ssm.model.ShiroUser;
import com.javaxl.ssm.service.ShiroUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author 小李飞刀
 * @site www.javaxl.com
 * @company
 * @create  2019-10-12 15:54
 */
@Service("shiroUserService")
public class ShiroUserServiceImpl implements ShiroUserService {
    @Autowired
    private ShiroUserMapper shiroUserMapper;
    @Override
    public Set<String> getRolesByUserId(Integer userId) {
        return shiroUserMapper.getRolesByUserId(userId);
    }

    @Override
    public Set<String> getPersByUserId(Integer userId) {
        return shiroUserMapper.getPersByUserId(userId);
    }

    @Override
    public ShiroUser queryByName(String userName) {
        return shiroUserMapper.queryByName(userName);
    }
}

 

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

@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;
    }


注解式开发

常用注解介绍

 

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

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

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

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

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

 

 

注解的使用

 

Controller

@RequiresUser
@RequestMapping("/passUser")
public String passUser(HttpServletRequest request){
    return "admin/addUser";
}

@RequiresRoles(value = {"1","4"},logical = Logical.AND)
@RequestMapping("/passRole")
public String passRole(HttpServletRequest request){
    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";
}

 

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>


main.jsp

小李飞刀_shiro


预测结果

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

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

zdm可以看所有按钮的内容


关键字:     安全框架       Shiro  

备案号:湘ICP备19000029号

Copyright © 2018-2019 javaxl晓码阁 版权所有