springboot整合Shiro鉴权

springboot整合Shiro鉴权

先写一个自定义的realm类

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.HashSet;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {
    //鉴权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //登录用户名
        String  username = (String) principals.getPrimaryPrincipal();
        //根据昵称查询数据库中所有权限  
        //PermMapper  List list=permMapper.findALl();

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

		//在此处进行数据库查询,查询到此用户的数据
        //模拟从数据库中查到的权限
        Set set=new HashSet<>();
        set.add("findAll");
        set.add("insert");

		//将查询到的权限设置到simpleAuthorizationInfo里面,返回
        simpleAuthorizationInfo.setStringPermissions(set);
        return simpleAuthorizationInfo;
    }


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获得登录名
        String username = (String) token.getPrincipal();
        //从数据库中获得密码
        //1。直接使用 ini 文件模拟数据库,只定义 [users]用户信息和[roles]角色权限信息;使用默认认证器;
        //2。自定义realm方式
        /*
        第一种:直接使用原文密码相比较
        只需将自定义realm替换默认认证器即可;
         */
        //666
        /*
        第二种:自定义凭证器;
        设置密码比较的方式:使用md5,可设置加密的次数;
         */
        //fae0b27c451c728867a567e8c1bb4e53
        /*
        第三种:自定义凭证器:
        设置密码加盐的比较方式;
        返回含有盐值的 SimpleAuthenticationInfo对象;
         */
        //104a525cd707f3275e6500260758f9e7
        String password="104a525cd707f3275e6500260758f9e7";
        //定义盐值
        ByteSource salt = ByteSource.Util.bytes("j2210");
        //返回数据的封装对象
//        return new SimpleAuthenticationInfo(username,password,this.getName());
        return new SimpleAuthenticationInfo(username,password,salt,this.getName());
    }
}

再写一个shiro配置类

@Configuration
public class ShiroConfig{
	//ShiroFilterFactoryBean
	 @Bean  //交给springioc
public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
 ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //权限的设置
        Map map=new HashMap<>();
        map.put("/findAll","perms[findAll]");
        map.put("/insert","perms[insert]");
        map.put("/delete","perms[delete]");
//        map.put("/update","perms[update]");// /cart  /goods
        shiroFilterFactoryBean.setLoginUrl("/url/login"); //没有登陆 进入此链接
        shiroFilterFactoryBean.setUnauthorizedUrl("/url/unauth");   //没有权限,进入此链接
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);//拦截map里key路径
        return shiroFilterFactoryBean;
}

	//DefaultWebSecurityManager
	 @Bean(name="securityManager")
public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("myrealm") MyRealm realm){
  DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
  //关联自定义的realm
  defaultWebSecurityManager.setRealm(realm);
  return defaultWebSecurityManager;
}

	//Realm
	//将自定义realm的实例交给springioc
	 @Bean(name = "myrealm")
public MyRealm myRealm(){
  MyRealm myRealm = new MyRealm();
  //设置凭证比较器
  HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher("md5");
  //设置任证比较器的相关属性
  credentialsMatcher.setHashIterations(1);
  //设置认证器的凭证比较器
  myRealm.setCredentialsMatcher(credentialsMatcher);
  return myRealm;
}
}

注意:

//总的来说:就是给shiro安全环境securityManager 配置一个自定义的realm来替换自定义的
//realm,在realm中进行自定义的鉴权和认证
//在认证的时候为了保护密码,对密码进行md5加密,所以需要给realm配置一个凭证比较器来加密
//由于我们依然需要用SecurityUtils.getSubject();来创建主体,所以我们需要给shiro工厂配置上 环境,让spring来管理,让我们在创建主题的的配置起效.

你可能感兴趣的:(spring,boot,java,spring)