天天看點

SpringBoot整合Shiro(認證+授權)

文章目錄

    • Shiro架構簡介
      • Spring Boot整合shiro環境搭建
      • Shiro實作登入攔截

Shiro架構簡介

Apache Shiro是一個強大且易用的Java安全架構,執行身份認證丶授權丶密碼和會話管理。

shiro有6大作用:

  1. Authentcation 認證===使用者登入
  2. Authorization 授權===使用者具有哪些權限
  3. Cryptography 安全資料加密
  4. Session Management 會話管理
  5. Web integration web 系統內建
  6. Interations 內建其他應用,spring,緩存架構

    主要是 認證 和 授權

以使用者登入為例-多圖參考↓

SpringBoot整合Shiro(認證+授權)
SpringBoot整合Shiro(認證+授權)

Spring Boot整合shiro環境搭建

1.建立Spring Boot應用,內建Shiro及相關元件,pom.xml

SpringBoot整合Shiro(認證+授權)

pom.xml

<dependency>
				<groupId>org.apache.shiro</groupId>
				<artifactId>shiro-spring</artifactId>
				<version>1.7.1</version>
    	</dependency>
    	<dependency>
				<groupId>com.baomidou</groupId>
				<artifactId>mybatis-plus-boot-starter</artifactId>
				<version>3.4.2</version>
    	</dependency>
    	<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
    	</dependency>
           

2.準備一個sql表

SpringBoot整合Shiro(認證+授權)

3.配置yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shirodbtest?account
    username: root
    password: guohui
  thymeleaf:
  prefix: classpath:/templates
           

4.自定義realm

自定義realm首先我們就要寫一個realm,而這個realm我們一般要繼承AuthorizingRealm類,

因為這個類裡面就有實作接收使用者認證資訊和接收使用者權限資訊的兩個方法,而realm就是用來從資料庫查詢這些資料的。

下面是我自定義的realm:

//。1 自定義的Realm
public class UserRealm extends AuthorizingRealm {
    @Override//授權
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("執行了授權===》doGetAuthorizationInfo");
        return null;
    }

    @Override//認證
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("執行了認證===》doGetAuthenticationInfo");
        return null;
    }
}
           

5.編寫controller控制器

@Controller
public class MyController {
    @RequestMapping("/index")
    public String toIndex(Model model){
        model.addAttribute("msgTest","hello,shiro");
        return "index";
    }
    @RequestMapping("user/add")
    public String add(){
        return "user/add";
    }
    @RequestMapping("user/update")
    public String update(){
        return "user/update";
    }
}
           

6.編寫Shiro的配置類

@Configuration
public class ShiroConfig {
    //3. 連接配接前端  ShiroFilterFactoryBean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //設定安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        return bean;
    }

    //2. 接管對象  DafaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //關聯userReal   接管reaml對象
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //1. 建立realm對象 、需要自定義
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }
}
           

7.controller對應的界面

index

SpringBoot整合Shiro(認證+授權)

add

SpringBoot整合Shiro(認證+授權)

update

SpringBoot整合Shiro(認證+授權)

8.主啟動器 啟動 并通路測試 http://localhost:8080

SpringBoot整合Shiro(認證+授權)

跳轉add和update測試

SpringBoot整合Shiro(認證+授權)
SpringBoot整合Shiro(認證+授權)

Spring Boot整合Shiro環境搭建成功

Shiro實作登入攔截

1.編寫登入界面

SpringBoot整合Shiro(認證+授權)

2.編寫登入界面的controller

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

3.Realm配置過濾器

Realm内置過濾器

anon: 無需認證就可以通路

authc: 必須認證了才能通路

user: 必須擁有我 記住我 功能才能通路

perms: 擁有對莫個資源的權限才能通路

role: 擁有莫個角色權限才能通路

@Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //設定安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        /* 添加Shiro的内置過濾器
            anon: 無需認證就可以通路
            authc: 必須認證了才能通路
            user:  必須擁有我 記住我 功能才能通路
            perms: 擁有對莫個資源的權限才能通路
            role:  擁有莫個角色權限才能通路
         */
        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/user/add", "anon");//user,的add anon設定所有人可以通路
        filterMap.put("/user/update", "authc");//user,的update authc設定認證了才能通路

        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/tologin");
        return bean;
    }
           

4.測試

我們給add界面設定的anon 無需認證就可以通路

我們給update界面設定的authc: 必須認證了才能通路

通路update界面設定的是認證了才能通路

我們設定了登入攔截

沒有權限通路的界面都會被登入界面攔截

SpringBoot整合Shiro(認證+授權)

通路update功能

SpringBoot整合Shiro(認證+授權)

Shiro實作登入攔截成功!!!

#springboot+shiro+mybatis-plus+thymeleaf整合

springboot+shiro+mybatis-plus+thymeleaf整合

繼續閱讀