最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于spring boot 2和shiro實現身份驗證案例

 更新時間:2020年04月23日 09:15:15   投稿:yaominghui  
這篇文章主要介紹了基于spring boot 2和shiro實現身份驗證案例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Shiro是一個功能強大且易于使用的Java安全框架,官網:https://shiro.apache.org/。

主要功能有身份驗證、授權、加密和會話管理。

其它特性有Web支持、緩存、測試支持、允許一個用戶用另一個用戶的身份進行訪問、記住我。

Shiro有三個核心組件:Subject,SecurityManager和 Realm。

Subject:即當前操作“用戶”,“用戶”并不僅僅指人,也可以是第三方進程、后臺帳戶或其他類似事物。

SecurityManager:安全管理器,Shiro框架的核心,通過SecurityManager來管理所有Subject,并通過它來提供安全管理的各種服務。

Realm:域,充當了Shiro與應用安全數據間的“橋梁”或者“連接器”。也就是說,當對用戶執(zhí)行認證(登錄)和授權(訪問控制)驗證時,Shiro會從應用配置的Realm中查找用戶及其權限信息。當配置Shiro時,必須至少指定一個Realm,用于認證和(或)授權。

Spring Boot 中整合Shiro,根據引入的依賴包shiro-spring和shiro-spring-boot-web-starter(當前版本都是1.4.2)不同有兩種不同方法。

方法一:引入依賴包shiro-spring

1、IDEA中創(chuàng)建一個新的SpringBoot項目,pom.xml引用的依賴包如下:

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

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.4.2</version>
    </dependency>

2、創(chuàng)建Realm和配置shiro

(1)創(chuàng)建Realm

package com.example.demo.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 MyRealm extends AuthorizingRealm {

  /**權限信息,暫不實現*/
  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    return null;
  }

  /**身份認證:驗證用戶輸入的賬號和密碼是否正確。*/
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    //獲取用戶輸入的賬號
    String userName = (String) token.getPrincipal();
    //驗證用戶admin和密碼123456是否正確
    if (!"admin".equals(userName)) {
      throw new UnknownAccountException("賬戶不存在!");
    }
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, "123456", getName());
    return authenticationInfo;
    //實際項目中,上面賬號從數據庫中獲取用戶對象,再判斷是否存在
    /*User user = userService.findByUserName(userName);
    if (user == null) {
      throw new UnknownAccountException("賬戶不存在!");
    }
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(), getName());
    return authenticationInfo;
    */
  }
}

(2)配置Shiro

package com.example.demo.config;

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

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

@Configuration
public class ShiroConfig {
  @Bean
  MyRealm myRealm() {
    return new MyRealm();
  }

  @Bean
  DefaultWebSecurityManager securityManager() {
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(myRealm());
    return manager;
  }

  @Bean
  ShiroFilterFactoryBean shiroFilterFactoryBean() {
    ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
    bean.setSecurityManager(securityManager());
    //如果不設置默認會自動尋找Web工程根目錄下的"/login.jsp"頁面
    bean.setLoginUrl("/login");
    //登錄成功后要跳轉的鏈接
    bean.setSuccessUrl("/index");
    //未授權界面
    bean.setUnauthorizedUrl("/403");
    //配置不會被攔截的鏈接
    Map<String, String> map = new LinkedHashMap<>();
    map.put("/doLogin", "anon");
    map.put("/**", "authc");
    bean.setFilterChainDefinitionMap(map);
    return bean;
  }
}

3、控制器測試方法

package com.example.demo.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

  @GetMapping("/login")
  public String login() {
    return "登錄頁面...";
  }

  @PostMapping("/doLogin")
  public String doLogin(String userName, String password) {
    Subject subject = SecurityUtils.getSubject();
    try {
      subject.login(new UsernamePasswordToken(userName, password));
      return "登錄成功!";
    } catch (UnknownAccountException e) {
      return e.getMessage();
    } catch (AuthenticationException e) {
      return "登陸失敗,密碼錯誤!";
    }
  }

  //如果沒有先登陸,訪問會跳到/login
  @GetMapping("/index")
  public String index() {
    return "index";
  }

  @GetMapping("/403")
  public String unauthorizedRole(){
    return "沒有權限";
  }
}

方法二:引入依賴包shiro-spring-boot-web-starter

1、pom.xml中刪除shiro-spring,引入shiro-spring-boot-web-starter

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

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring-boot-web-starter</artifactId>
      <version>1.4.2</version>
    </dependency>

2、創(chuàng)建Realm和配置shiro

(1)創(chuàng)建Realm,代碼和方法一的一樣。

(2)配置Shiro

package com.example.demo.config;

import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
  @Bean
  MyRealm myRealm() {
    return new MyRealm();
  }

  @Bean
  DefaultWebSecurityManager securityManager() {
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(myRealm());
    return manager;
  }

  @Bean
  ShiroFilterChainDefinition shiroFilterChainDefinition() {
    DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
    definition.addPathDefinition("/doLogin", "anon");
    definition.addPathDefinition("/**", "authc");
    return definition;
  }
}

(3)application.yml配置

shiro:
 unauthorizedUrl: /403
 successUrl: /index
 loginUrl: /login

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • servlet生命周期_動力節(jié)點Java學院整理

    servlet生命周期_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了servlet生命周期的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • SpringBoot中的Controller用法示例詳解

    SpringBoot中的Controller用法示例詳解

    Controller是SpringBoot里最基本的組件,他的作用是把用戶提交來的請求通過對URL的匹配,分配給不同的接收器,再進行處理,然后向用戶返回結果,這篇文章主要介紹了SpringBoot中的Controller用法,需要的朋友可以參考下
    2023-06-06
  • SpringBoot聲明式事務的簡單運用說明

    SpringBoot聲明式事務的簡單運用說明

    這篇文章主要介紹了SpringBoot聲明式事務的簡單運用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Java @SentinelResource全面介紹

    Java @SentinelResource全面介紹

    在實際應用過程中,我們可能需要限流的層面不僅限于接口??赡軐τ谀硞€方法的調用限流,對于某個外部資源的調用限流等都希望做到控制。對此,我們需要學習使用@SentinelResource注解,靈活的定義控制資源以及如何配置控制策略
    2022-08-08
  • idea前后跳轉箭頭的快捷鍵

    idea前后跳轉箭頭的快捷鍵

    這篇文章主要介紹了idea前后跳轉箭頭的快捷鍵,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java 創(chuàng)建URL的常見問題及解決方案

    Java 創(chuàng)建URL的常見問題及解決方案

    這篇文章主要介紹了Java 創(chuàng)建URL的常見問題及解決方案的相關資料,需要的朋友可以參考下
    2016-10-10
  • maven中profile動態(tài)打包不同環(huán)境配置文件的實現

    maven中profile動態(tài)打包不同環(huán)境配置文件的實現

    開發(fā)項目時會遇到這個問題:開發(fā)環(huán)境,測試環(huán)境,生產環(huán)境的配置文件不同, 打包時經常要手動更改配置文件,本文就來介紹一下maven中profile動態(tài)打包不同環(huán)境配置文件的實現,感興趣的可以了解一下
    2023-10-10
  • Java如何將ResultSet結果集遍歷到List中

    Java如何將ResultSet結果集遍歷到List中

    這篇文章主要介紹了Java如何將ResultSet結果集遍歷到List中問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Netty分布式server啟動流程Nio創(chuàng)建源碼分析

    Netty分布式server啟動流程Nio創(chuàng)建源碼分析

    這篇文章主要介紹了Netty分布式server啟動流程Nio創(chuàng)建源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • SpringBoot項目Pom文件的基本配置方式

    SpringBoot項目Pom文件的基本配置方式

    這篇文章主要介紹了SpringBoot項目Pom文件的基本配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評論

浦北县| 方城县| 罗山县| 会同县| 陵川县| 徐水县| 依安县| 丹阳市| 土默特左旗| 岳池县| 新竹市| 三江| 泽州县| 右玉县| 吴旗县| 罗城| 奇台县| 阿图什市| 香港 | 阿坝| 宣威市| 景德镇市| 静乐县| 惠水县| 广丰县| 南宫市| 道孚县| 大洼县| 诸城市| 兴安县| 平安县| 瑞安市| 武平县| 高要市| 凤庆县| 竹北市| 法库县| 肥乡县| 遵化市| 商都县| 伽师县|