Java安全框架——Shiro的使用详解(附springboot整合Shiro的demo)

Shiro简介

  1. Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理
  2. 三个核心组件:Subject, SecurityManager 和 Realms
  3. Subject代表了当前用户的安全操作
  4. SecurityManager管理所有用户的安全操作,是Shiro框架的核心,Shiro通过SecurityManager来管理内部组件实例,并通过它来提供安全管理的各种服务。
  5. Realm充当了Shiro与应用安全数据间的“桥梁”或者“连接器”。也就是说,当对用户执行认证(登录)和授权(访问控制)验证时,Shiro会从应用配置的Realm中查找用户及其权限信息。
  6. Realm实质上是一个安全相关的DAO:它封装了数据源的连接细节,并在需要时将相关数据提供给Shiro。当配置Shiro时,你必须至少指定一个Realm,用于认证和(或)授权。配置多个Realm是可以的,但是至少需要一个。

Shiro快速入门

导入依赖

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.7.1</version>
        </dependency>

        <!-- configure logging -->
        <!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>2.0.0-alpha1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>2.0.0-alpha1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

配置log4j.properties

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n

# General Apache libraries
log4j.logger.org.apache=WARN

# Spring
log4j.logger.org.springframework=WARN

# Default Shiro logging
log4j.logger.org.apache.shiro=INFO

# Disable verbose logging
log4j.logger.org.apache.shiro.util.ThreadContext=WARN
log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN

配置Shiro.ini(在IDEA中需要导入ini插件)

[users]
# user 'root' with password 'secret' and the 'admin' role
root = secret, admin
# user 'guest' with the password 'guest' and the 'guest' role
guest = guest, guest
# user 'presidentskroob' with password '12345' ("That's the same combination on
# my luggage!!!" ;)), and role 'president'
presidentskroob = 12345, president
# user 'darkhelmet' with password 'ludicrousspeed' and roles 'darklord' and 'schwartz'
darkhelmet = ludicrousspeed, darklord, schwartz
# user 'lonestarr' with password 'vespa' and roles 'goodguy' and 'schwartz'
lonestarr = vespa, goodguy, schwartz

# -----------------------------------------------------------------------------
# Roles with assigned permissions
#
# Each line conforms to the format defined in the
# org.apache.shiro.realm.text.TextConfigurationRealm#setRoleDefinitions JavaDoc
# -----------------------------------------------------------------------------
[roles]
# 'admin' role has all permissions, indicated by the wildcard '*'
admin = *
# The 'schwartz' role can do anything (*) with any lightsaber:
schwartz = lightsaber:*
# The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with
# license plate 'eagle5' (instance specific id)
goodguy = winnebago:drive:eagle5

快速入门实现类 quickStart.java

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class quickStart {

    private static final transient Logger log = LoggerFactory.getLogger(quickStart.class);

    /*
        Shiro三大对象:
                Subject: 用户
                SecurityManager:管理所有用户
                Realm: 连接数据
     */

    public static void main(String[] args) {

        // 创建带有配置的Shiro SecurityManager的最简单方法
        // realms, users, roles and permissions 是使用简单的INI配置。
        // 我们将使用可以提取.ini文件的工厂来完成此操作,
        // 返回一个SecurityManager实例:

        // 在类路径的根目录下使用shiro.ini文件
        // (file:和url:前缀分别从文件和url加载):
        //Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        //SecurityManager securityManager = factory.getInstance();
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        IniRealm iniRealm = new IniRealm("classpath:shiro.ini");
        securityManager.setRealm(iniRealm);

        // 对于这个简单的示例快速入门,请使SecurityManager
        // 可作为JVM单例访问。大多数应用程序都不会这样做
        // 而是依靠其容器配置或web.xml进行
        // webapps。这超出了此简单快速入门的范围,因此
        // 我们只做最低限度的工作,这样您就可以继续感受事物.
        SecurityUtils.setSecurityManager(securityManager);

        // 现在已经建立了一个简单的Shiro环境,让我们看看您可以做什么:

        // 获取当前用户对象 Subject
        Subject currentUser = SecurityUtils.getSubject();

        // 使用Session做一些事情(不需要Web或EJB容器!!!
        Session session = currentUser.getSession();//通过当前用户拿到Session
        session.setAttribute("someKey", "aValue");
        String value = (String) session.getAttribute("someKey");
        if (value.equals("aValue")) {
            log.info("Retrieved the correct value! [" + value + "]");
        }

        // 判断当前用户是否被认证
        if (!currentUser.isAuthenticated()) {
            //token : 令牌,没有获取,随机
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            token.setRememberMe(true); // 设置记住我
            try {
                currentUser.login(token);//执行登陆操作
            } catch (UnknownAccountException uae) {//打印出  用户名
                log.info("There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {//打印出 密码
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
            }
            // ... 在此处捕获更多异常(也许是针对您的应用程序的自定义异常?
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            }
        }

        //say who they are:
        //print their identifying principal (in this case, a username):
        log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

        //test a role:
        if (currentUser.hasRole("schwartz")) {
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

        //test a typed permission (not instance-level)
        if (currentUser.isPermitted("lightsaber:wield")) {
            log.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            log.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

        //a (very powerful) Instance Level permission:
        if (currentUser.isPermitted("winnebago:drive:eagle5")) {
            log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                    "Here are the keys - have fun!");
        } else {
            log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

        //all done - log out!
        currentUser.logout();//注销

        System.exit(0);//退出
    }
}

启动测试

SpringBoot-Shiro整合(最后会附上完整代码)

前期工作

导入shiro-spring整合包依赖

<!--  shiro-spring整合包 -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>1.7.1</version>
</dependency>

跳转的页面
index.html

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>

<h1>首页</h1>

<p th:text="${msg}"></p>

<a th:href="@{/user/add}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >add</a>| <a th:href="@{/user/update}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >update</a>

</body>
</html>

add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>add</title>
</head>
<body>
<p>add</p>
</body>
</html>

update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>update</title>
</head>
<body>
<p>update</p>
</body>
</html>

编写shiro的配置类ShiroConfig.java

package com.example.config;

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 {
    //3. ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getshiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        factoryBean.setSecurityManager(defaultWebSecurityManager);

        return factoryBean;
    }

    //2.创建DefaultWebSecurityManager
    @Bean(name = "SecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager SecurityManager=new DefaultWebSecurityManager();
        //3.关联Realm
        SecurityManager.setRealm(userRealm);
        return SecurityManager;
    }
    //1.创建Realm对象
    @Bean(name = "userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }

}

编写UserRealm.java

package com.example.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");
        return null;
    }
}

编写controller测试环境是否搭建好

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {

    @RequestMapping({"/","/index"})
    public String index(Model model){
        model.addAttribute("msg","hello,shiro");
        return "index";
    }

    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }

    @RequestMapping("/user/update")
    public String update(){
        return "user/update";
    }
}

实现登录拦截

在ShiroConfig.java文件中添加拦截

Map<String,String> filterMap = new LinkedHashMap<>();
        //对/user/*下的文件只有拥有authc权限的才能访问
        filterMap.put("/user/*","authc");
        //将Map存放到ShiroFilterFactoryBean中
        factoryBean.setFilterChainDefinitionMap(filterMap);

这样,代码跑起来,你点击add或者update就会出现404错误,这时候,我们再继续添加,让它跳转到我们自定义的登录页

添加登录拦截到登录页

        //需进行权限认证时跳转到toLogin
        factoryBean.setLoginUrl("/toLogin");
        //权限认证失败时跳转到unauthorized
        factoryBean.setUnauthorizedUrl("/unauthorized");

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<form action="">
    用户名:<input type="text" name="username"><br>
    密码:<input type="text" name="password"><br>
    <input type="submit">
</form>

</body>
</html>

视图跳转添加一个login页面跳转

    @RequestMapping("/toLogin")
    public String login(){
        return "login";
    }

上面,我们已经成功拦截了,现在我们来实现用户认证

首先,我们需要一个登录页面

login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<p th:text="${msg}" style="color: red"></p>
<form th:action="@{/login}">
    用户名:<input type="text" name="username"><br>
    密码:<input type="text" name="password"><br>
    <input type="submit">
</form>

</body>
</html>

其次,去controller编写跳转到登录页面

    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        //获得当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken taken = new UsernamePasswordToken(username,password);

        try{//执行登陆操作,没有发生异常就说明登陆成功
            subject.login(taken);
            return "index";
        }catch (UnknownAccountException e){
            model.addAttribute("msg","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }

最后去UserRealm.java配置认证

   //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");

        String name = "root";
        String password = "123456";

        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;

        if (!userToken.getUsername().equals(name)){
            return null;//抛出异常  用户名错误那个异常
        }

        //密码认证,shiro自己做
        return new SimpleAuthenticationInfo("",password,"");
    }

运行测试,成功!!!

附上最后的完整代码

pom.xml引入的依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-08-shiro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-08-shiro</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!--  shiro-spring整合包 -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

静态资源

index.html

<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>

<h1>首页</h1>

<p th:text="${msg}"></p>

<a th:href="@{/user/add}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >add</a>| <a th:href="@{/user/update}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >update</a>

</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<p th:text="${msg}" style="color: red"></p>
<form th:action="@{/login}">
    用户名:<input type="text" name="username"><br>
    密码:<input type="text" name="password"><br>
    <input type="submit">
</form>

</body>
</html>

add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>add</title>
</head>
<body>
<p>add</p>
</body>
</html>

update.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>update</title>
</head>
<body>
<p>update</p>
</body>
</html>

controller层

MyController.java

package com.example.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyController {

    @RequestMapping({"/","/index"})
    public String index(Model model){
        model.addAttribute("msg","hello,shiro");
        return "index";
    }

    @RequestMapping("/user/add")
    public String add(){
        return "user/add";
    }

    @RequestMapping("/user/update")
    public String update(){
        return "user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username,String password,Model model){
        //获得当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken taken = new UsernamePasswordToken(username,password);

        try{//执行登陆操作,没有发生异常就说明登陆成功
            subject.login(taken);
            return "index";
        }catch (UnknownAccountException e){
            model.addAttribute("msg","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }

}

config文件

ShiroConfig.java

package com.example.config;

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 {
    //4. ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getshiroFilterFactoryBean(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //5. 设置安全管理器
        factoryBean.setSecurityManager(defaultWebSecurityManager);

        /*  shiro内置过滤器
            anon	无需授权、登录就可以访问,所有人可访。
            authc	 需要登录授权才能访问。
            authcBasic	Basic HTTP身份验证拦截器
            logout	退出拦截器。退出成功后,会 redirect到设置的/URI
            noSessionCreation	不创建会话连接器
            perms	授权拦截器,拥有对某个资源的权限才可访问
            port	端口拦截器
            rest	rest风格拦截器
            roles	角色拦截器,拥有某个角色的权限才可访问
            ssl	ssl拦截器。通过https协议才能通过
            user	用户拦截器,需要有remember me功能方可使用
         */
        Map<String,String> filterMap = new LinkedHashMap<>();
        //对/user/*下的文件只有拥有authc权限的才能访问
        filterMap.put("/user/*","authc");
        //将Map存放到ShiroFilterFactoryBean中
        factoryBean.setFilterChainDefinitionMap(filterMap);
        //需进行权限认证时跳转到toLogin
        factoryBean.setLoginUrl("/toLogin");
        //权限认证失败时跳转到unauthorized
        factoryBean.setUnauthorizedUrl("/unauthorized");

        return factoryBean;
    }

    //2.创建DefaultWebSecurityManager
    @Bean(name = "SecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager SecurityManager=new DefaultWebSecurityManager();
        //3.关联Realm
        SecurityManager.setRealm(userRealm);
        return SecurityManager;
    }
    //1.创建Realm对象
    @Bean(name = "userRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }

}

UserRealm.java

package com.example.config;

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

public class UserRealm extends AuthorizingRealm {

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");

        String name = "root";
        String password = "123456";

        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;

        if (!userToken.getUsername().equals(name)){
            return null;//抛出异常  用户名错误那个异常
        }

        //密码认证,shiro自己做
        return new SimpleAuthenticationInfo("",password,"");
    }
}

但是,我们在用户认证这里,真实情况是从数据库中取的,所以,我们接下来去实现一下从数据库中取出数据来实现用户认证

Shiro整合mybatis

前期工作

在前面导入的依赖中,继续添加以下依赖

        <!--  mysql      -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--   log4j     -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!--  数据源Druid      -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.5</version>
        </dependency>
        <!--   引入mybatis     -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--  lombok      -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

导入了mybatis和Druid,就去application.properties配置一下和Druid
Druid

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源

    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

mybatis

mybatis:
  type-aliases-package: com.example.pojo
  mapper-locations: classpath:mapper/*.xml

连接数据库
编写实体类

package com.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
        private Integer id;
        private String name;
        private String pwd;
}

编写mapper

package com.example.mapper;

import com.example.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface UserMapper {
    public User getUserByName(String name);
}

编写mapper.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.mapper.UserMapper">

    <select id="getUserByName" parameterType="String" resultType="User">
        select * from mybatis.user where name=#{name}
    </select>

</mapper>

编写service

package com.example.service;

import com.example.pojo.User;

public interface UserService {
    public User getUserByName(String name);
}
package com.example.service;

import com.example.mapper.UserMapper;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }
}

使用数据库中的数据

修改UserRealm.java即可

package com.example.config;

import com.example.pojo.User;
import com.example.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");

        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;

        //连接真实的数据库
        User user = userService.getUserByName(userToken.getUsername());

        if (user==null){
            return null;//抛出异常  用户名错误那个异常
        }

        //密码认证,shiro自己做
        return new SimpleAuthenticationInfo("",user.getPwd(),"");
    }
}

认证搞完了,我们再来看看授权

在ShiroConfig.java文件加入授权,加入这行代码: filterMap.put("/user/add","perms[user:add]");//只有拥有user:add权限的人才能访问add,注意授权的位置在认证前面,不然授权会认证不了;

运行测试:add页面无法访问

授权同理:filterMap.put("/user/update","perms[user:update]");//只有拥有user:update权限的人才能访问update

自定义一个未授权跳转页面

在ShiroConfig.java文件设置未授权时跳转到unauthorized页面,加入这行代码:
factoryBean.setUnauthorizedUrl("/unauthorized"); 2. 去Mycontroller写跳转未授权页面

    @RequestMapping("/unauthorized")
    @ResponseBody//懒得写界面,返回一个字符串
    public String unauthorized(){
        return "没有授权,无法访问";
    }

运行效果:

从数据库中接受用户的权限,进行判断

在数据库中添加一个属性perms,相应的实体类也要修改

修改UserRealm.java

package com.example.config;

import com.example.pojo.User;
import com.example.service.UserService;
import org.apache.shiro.SecurityUtils;
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;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        //没有使用数据库,直接自己设置的用户权限,给每个人都设置了,现实中要从数据库中取
        //info.addStringPermission("user:add");

        //从数据库中得到权限信息
        //获得当前登录的对象
        Subject subject = SecurityUtils.getSubject();
        //拿到User对象,通过getPrincipal()获得
        User currentUser = (User) subject.getPrincipal();

        //设置当前用户的权限
        info.addStringPermission(currentUser.getPerms());

        return info;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");

        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;

        //连接真实的数据库
        User user = userService.getUserByName(userToken.getUsername());

        if (user==null){
            return null;//抛出异常  用户名错误那个异常
        }

        //密码认证,shiro自己做
        return new SimpleAuthenticationInfo(user,user.getPwd(),"");
    }
}

有了授权后,就又出现了一个问题,我们是不是要让用户没有权限的东西,就看不见呢?这时候,就出现了Shiro-thymeleaf整合

Shiro-thymeleaf整合

导入整合的依赖

<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
<dependency>
   <groupId>com.github.theborakompanioni</groupId>
    <artifactId>thymeleaf-extras-shiro</artifactId>
    <version>2.0.0</version>
</dependency>

在ShiroConfig整合ShiroDialect

    //整合ShiroDialect: 用来整合 shiro thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return  new ShiroDialect();
    }

修改index页面

<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro">
<!-- 三个命名空间
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"
-->
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>

<h1>首页</h1>

<p th:text="${msg}"></p>

<!--判断是否有用户登录,如果有就不显示登录按钮-->
<div th:if="${session.loginUser==null}">
    <a th:href="@{/toLogin}" rel="external nofollow" >登录</a>
</div>

<div shiro:hasPermission="user:add">
    <a th:href="@{/user/add}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/user/update}" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >update</a>
</div>

</body>
</html>

判断是否有用户登录

   //这个是整合shiro和thymeleaf用到的,让登录按钮消失的判断
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("loginUser", user);

测试

以上就是Java安全框架——Shiro的使用详解(附springboot整合Shiro的demo)的详细内容,更多关于Java安全框架——Shiro的资料请关注我们其它相关文章!

(0)

相关推荐

  • java mybatis框架配置详解

    一个框架的使用,必然离不开其中的组件支持.我们在下载完mybatis框架后,因为大部分的内部结构还没有启动,就要手动的对其进行配置.在之前有提到,mybatis框架的作用就有数据库方面的,所以本篇文章带来了数据库和sql方面的配置方法,大家一起往下面看看具体操作. 1.配置数据库 创建mybatis的配置文件,配置数据库的信息.数据库我们可以配置多个,但是默认的只能用一个. <?xml version="1.0" encoding="UTF-8"?> &

  • Java高并发测试框架JCStress详解

    前言 如果要研究高并发,一般会借助高并发工具来进行测试.JCStress(Java Concurrency Stress)它是OpenJDK中的一个高并发测试工具,它可以帮助我们研究在高并发场景下JVM,类库以及硬件等状况. JCStress学起来很简单,而且官方也提供了许多高并发场景下的测试用例,只要引入一个jar包,即可运行研究. 如何使用JCStress 此演示用maven工程,首先需要引入jar包,核心包是必须要的,样例包非必须要,此是为了演示其中的例子. <dependencies>

  • 手把手教你从零设计一个java日志框架

    输出内容 - LoggingEvent 提到日志框架,最容易想到的核心功能,那就是输出日志了.那么对于一行日志内容来说,应该至少包含以下几个信息: 日志时间戳 线程信息 日志名称(一般是全类名) 日志级别 日志主体(需要输出的内容,比如info(str)) 为了方便的管理输出内容,现在需要创建一个输出内容的类来封装这些信息: public class LoggingEvent { public long timestamp;//日志时间戳 private int level;//日志级别 pri

  • java swing框架实现贪吃蛇游戏

    本文实例为大家分享了java swing实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 1.编写main方法 package game; import java.awt.Graphics; import javax.swing.*; public class snakeMain extends JFrame { public snakeMain() { snakeWin win = new snakeWin(); add(win); setTitle("̰贪吃蛇v1.0"); se

  • 分享我的第一次java Selenium自动化测试框架开发过程

    由于公司的开发团队偏向于使用Java技术,而且公司倡导学习开源技术,所以我选择用Java语言来进行Selenium WebDriver的自动化框架开发.由于本人没有Java开发经验,以前虽然学过QTP但从没有接触过Selenium,正好通过这个机会能学习一下自动化测试,同时也学习一下基本的Java开发过程. 一.首先是搭建框架开发环境 按照网上的方法部署eclipse,建立TestAction工程,并Import引用JDK和Selenium-2.44完整包 二.继续引用和安装相关jar包 1.首

  • Micronaut框架的简单使用介绍

    什么是Micronaut Micronaut是一个基于JVM的框架,用于构建轻量级.模块化的应用程序.Micronaut是由创建Grails的同一家公司OCI开发的最新框架,旨在使创建微服务变得快速和简单. 虽然Micronaut包含一些类似于Spring等现有框架的特性,但它也有一些新特性使它与众不同.通过对Java.Groovy和Kotlin的支持,它提供了多种创建应用程序的方法. 主要特点 Micronaut最令人兴奋的特性之一是它的编译时依赖注入机制.大多数框架使用反射和代理在运行时执行

  • java 非常好用的反射框架Reflections介绍

    Reflections通过扫描classpath,索引元数据,并且允许在运行时查询这些元数据. 使用Reflections可以很轻松的获取以下元数据信息: 1)获取某个类型的所有子类:比如,有一个父类是TestInterface,可以获取到TestInterface的所有子类. 2)获取某个注解的所有类型/字段变量,支持注解参数匹配. 3)使用正则表达式获取所有匹配的资源文件 4)获取特定签名方法. 通常的用法有: 引入依赖jar <dependency> <groupId>org

  • Java中JFinal框架动态切换数据库的方法

    需求:需要根据企业ID切换对应的数据库,同时,后期可动态增加数据库配置 JFinal框架中对于对于多数据源配置有两种方式: 1.通过配置文件配置,有多少数据库就要配置多少,服务启动时加载所有数据库,缺点:不能动态增加数据库 2.只配置一个主数据库信息就可以了,其他数据库信息保存在表中,通过读取表数据加载数据库连接,优点:在数据表中增加数据库配置即可动态增加数据库连接. 本次主要介绍第2种方法: 一.新建数据表:保存数据库连接信息 配置表对应的实体类 public class DbDto { /*

  • 使用JAVA+Maven+TestNG框架实现超详细Appium测试安卓真机教程

    前言:前段时间做了selenium的学习和实践,有点意犹未尽,所以自己就又学了下Appium的使用,因为这一套东西在16年已经停止维护了,不管实现还是设计上都不是很容易,也踩了很多坑,现在在此记录下大概过程.后续有时间再完善手册. 一.准备 安装SDK,配置环境变量 链接: https://pan.baidu.com/s/1g2QaWjdfg6Txa0gZf9kk3A 提取码: 8aaz windows配置环境SDK变量 我的电脑右键->属性 点击高级系统设置 点击环境变量 点击新建按钮,变量名

  • Java爬虫框架之WebMagic实战

    一.介绍 WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个高效.易维护的爬虫. 二.如何学习 1.查看官网 官网地址为:http://webmagic.io/ 官网详细文档:http://webmagic.io/docs/zh/ 2.跑通hello world示例(具体可以参考官网,也可以参考博客) 我下面写的单元测试案例,可作为Hello World示例. 注意需要导入Maven依赖: <dependency> <groupId>us.

随机推荐