springboot-shiro整合

1、springboot项目创建

1-1、pom.xml中添加依赖



    org.springframework.boot
    spring-boot-starter-web




    org.springframework.boot
    spring-boot-starter-thymeleaf



    org.springframework.boot
    spring-boot-starter-test
    test

1-2、创建相关测试文件

image.png

2、整合shiro

2-1、pom.xml中添加依赖



    org.apache.shiro
    shiro-spring
    1.4.1

2-2、创建UserRealm

package com.mracale.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;


public class UserRealm extends AuthorizingRealm {

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("执行了授权-AuthorizationInfo:"+principals);
        //授予当前用户拥有的权限信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取下方传过来的数据,可能是权限数据  或者  用户角色信息
        String perm = (String) getAvailablePrincipal(principals);
        //添加拥有的权限数据
        info.addStringPermission(perm);
        return info;
    }

    //认证 (登录操作)
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了认证-AuthenticationInfo");
        //固定的用户名密码,后期数据中取
        String name="admin";
        String password="123";
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        if (!userToken.getUsername().equals(name)){
            return null;// UnknownAccountException
        }
        String principal = "user:add";
        //密码认证
        return new SimpleAuthenticationInfo(principal,password,"");
    }
}

2-3、创建ShiroConfig

package com.mracale.config;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultSecurityManager defaultSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);

        //添加shiro的内置过滤器
        /**
         * anon: 无需认证即可访问
         * authc: 必须认证才能访问
         * user: 必须拥有 记住我 功能才能访问
         * perms: 拥有对某个资源的权限才能访问
         * role: 拥有某个角色才能访问
         */

        Map filterMap = new LinkedHashMap<>();

        filterMap.put("/user/add","perms[user:add]");
        filterMap.put("/user/*","authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        //设置登录的请求
        shiroFilterFactoryBean.setLoginUrl("/login");
        //设置未授权的请求
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauth");

        return shiroFilterFactoryBean;
    }

    //DefaultWebSecurityManager
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建 realm 对象,需要自定义
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }
}

3、整合数据库操作

3-1、添加pom依赖


        
            mysql
            mysql-connector-java
        
        
            log4j
            log4j
        
        
            com.alibaba
            druid
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        

        
            org.projectlombok
            lombok
        

你可能感兴趣的:(springboot-shiro整合)