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

Springboot整合Shiro的代碼實(shí)例

 更新時(shí)間:2019年10月28日 10:27:18   作者:清水有丶白  
這篇文章主要介紹了Springboot整合Shiro的代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Springboot整合Shiro的代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、導(dǎo)入依賴

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

2、創(chuàng)建ShiroRealm.java文件

(這里按照需求,只做登錄認(rèn)證這塊)

package com.hyqfx.manager.shiro;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hyqfx.manager.entity.po.SystemAdmin;
import com.hyqfx.manager.service.ISystemAdminService;
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 ShiroRealm extends AuthorizingRealm {

  @Autowired
  private ISystemAdminService adminService;

  //授權(quán)
  @Override
  protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    /*
    //獲取登錄用戶名
    String name= (String) principalCollection.getPrimaryPrincipal();
    //查詢用戶名稱
    User user = loginService.findByName(name);
    //添加角色和權(quán)限
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    for (Role role:user.getRoles()) {
      //添加角色
      simpleAuthorizationInfo.addRole(role.getRoleName());
      for (Permission permission:role.getPermissions()) {
        //添加權(quán)限
        simpleAuthorizationInfo.addStringPermission(permission.getPermission());
      }
    }
    return simpleAuthorizationInfo;*/


    return null;
  }

  //認(rèn)證
  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    //加這一步的目的是在Post請(qǐng)求的時(shí)候會(huì)先進(jìn)認(rèn)證,然后在到請(qǐng)求
    if (authenticationToken.getPrincipal() == null) {
      return null;
    }
    //獲取用戶信息
    String name = authenticationToken.getPrincipal().toString(); 
    SystemAdmin admin = adminService.selectOne(new EntityWrapper<SystemAdmin>().eq("username",name));

    if (admin == null) {
      return null;
    } else {
      //這里驗(yàn)證authenticationToken和simpleAuthenticationInfo的信息
      SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(name, admin.getPassword().toString(), getName());
      return simpleAuthenticationInfo;
    }
  }
}

3、創(chuàng)建ShiroConfiguration.java文件

package com.becl.config;

import com.becl.shiro.PasswordMatcher;
import com.becl.shiro.ShiroRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
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.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfiguration {



  //將自己的驗(yàn)證方式加入容器
  @Bean
  public ShiroRealm myShiroRealm() {
    ShiroRealm myShiroRealm = new ShiroRealm();
    myShiroRealm.setCredentialsMatcher(passwordMatcher());//裝配自定義的密碼驗(yàn)證方式
    return myShiroRealm;
  }

  // 配置加密方式
  // 配置了一下,這貨就是驗(yàn)證不過(guò),,改成手動(dòng)驗(yàn)證算了,以后換加密方式也方便
  @Bean
  public PasswordMatcher passwordMatcher() {
    return new PasswordMatcher();
  }

  //權(quán)限管理,配置主要是Realm的管理認(rèn)證
  @Bean
  public SecurityManager securityManager() {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(myShiroRealm());
    return securityManager;
  }

  //Filter工廠,設(shè)置對(duì)應(yīng)的過(guò)濾條件和跳轉(zhuǎn)條件
  @Bean
  public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    Map<String,String> map = new HashMap<String, String>();
    //登出
    map.put("/logout","logout");
    //不需要認(rèn)證
    map.put("/logout","anon");
    map.put("/login*","anon");
    map.put("/shiroError","anon");
    //對(duì)所有用戶認(rèn)證
    map.put("/**","authc");
    //map.put("/**","anon");
    //登錄
    shiroFilterFactoryBean.setLoginUrl("/login");
    //首頁(yè)
    shiroFilterFactoryBean.setSuccessUrl("/index");
    //錯(cuò)誤頁(yè)面,認(rèn)證不通過(guò)跳轉(zhuǎn)
    shiroFilterFactoryBean.setUnauthorizedUrl("/shiroError");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
    return shiroFilterFactoryBean;
  }

  //加入注解的使用,不加入這個(gè)注解不生效
  @Bean
  public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
    AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
    authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
    return authorizationAttributeSourceAdvisor;
  }

}

4、自定義Shiro的密碼比較器

package com.becl.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.mindrot.jbcrypt.BCrypt;

/**
 * 自定義密碼比較器
 */
public class PasswordMatcher extends SimpleCredentialsMatcher {

  @Override
  public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    UsernamePasswordToken utoken=(UsernamePasswordToken) token;

    //獲得用戶輸入的密碼:(可以采用加鹽(salt)的方式去檢驗(yàn))
    String inPassword = new String(utoken.getPassword());
    String username = utoken.getUsername();

    //獲得數(shù)據(jù)庫(kù)中的密碼
    String dbPassword = (String) info.getCredentials();
    //進(jìn)行密碼的比對(duì)
    boolean flag = BCrypt.checkpw(inPassword,dbPassword);
    return flag;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java9以后的垃圾回收的具體用法

    Java9以后的垃圾回收的具體用法

    這篇文章主要介紹了Java9以后的垃圾回收的具體用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java軟件生產(chǎn)監(jiān)控工具Btrace使用方法詳解

    Java軟件生產(chǎn)監(jiān)控工具Btrace使用方法詳解

    這篇文章主要介紹了Java軟件生產(chǎn)監(jiān)控工具Btrace使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Intellij?IDEA如何查看所有斷點(diǎn)

    Intellij?IDEA如何查看所有斷點(diǎn)

    這篇文章主要介紹了Intellij?IDEA如何查看所有斷點(diǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷)

    解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷)

    這篇文章主要介紹了解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java分頁(yè)查詢的幾種實(shí)現(xiàn)方法舉例

    Java分頁(yè)查詢的幾種實(shí)現(xiàn)方法舉例

    這篇文章主要給大家介紹了關(guān)于Java分頁(yè)查詢的幾種實(shí)現(xiàn)方法,分頁(yè)是系統(tǒng)中常用到的功能,只要涉及到查詢必定伴隨而來(lái)的就是分頁(yè),文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • Java編程時(shí)間日期API實(shí)例解析

    Java編程時(shí)間日期API實(shí)例解析

    本文主要介紹了Java編程時(shí)間日期API實(shí)例解析的相關(guān)內(nèi)容,分享了一則實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2018-01-01
  • Java新手入門(mén)學(xué)習(xí)之正則表達(dá)式

    Java新手入門(mén)學(xué)習(xí)之正則表達(dá)式

    這篇文章主要給大家介紹了關(guān)于Java新手入門(mén)學(xué)習(xí)之正則表達(dá)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • java.lang.Void 與 void的比較及使用方法介紹

    java.lang.Void 與 void的比較及使用方法介紹

    這篇文章主要介紹了java.lang.Void 與 void的比較及使用方法介紹,小編覺(jué)得挺不錯(cuò)的,這里給大家分享一下,需要的朋友可以參考。
    2017-10-10
  • 淺析java中String類型中“==”與“equal”的區(qū)別

    淺析java中String類型中“==”與“equal”的區(qū)別

    這篇文章主要介紹了淺析java中String類型中“==”與“equal”的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringBoot實(shí)現(xiàn)發(fā)送短信的示例代碼

    SpringBoot實(shí)現(xiàn)發(fā)送短信的示例代碼

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)發(fā)送短信的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論

从江县| 交城县| 新野县| 板桥市| 兴城市| 江永县| 休宁县| 特克斯县| 玛纳斯县| 屏南县| 元阳县| 瑞昌市| 凌海市| 大英县| 达孜县| 浠水县| 房山区| 鄄城县| 神农架林区| 盖州市| 贵阳市| 曲水县| 宝应县| 介休市| 炎陵县| 资溪县| 凤翔县| 玉树县| 宁强县| 手机| 札达县| 镇平县| 静乐县| 桦川县| 防城港市| 华安县| 景东| 安岳县| 永济市| 公安县| 吐鲁番市|