欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

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

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

為西市等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及西市網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、西市網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

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請求的時(shí)候會(huì)先進(jìn)認(rèn)證,然后在到請求
    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)證不過,,改成手動(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è)置對應(yīng)的過濾條件和跳轉(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");
    //對所有用戶認(rèn)證
    map.put("/**","authc");
    //map.put("/**","anon");
    //登錄
    shiroFilterFactoryBean.setLoginUrl("/login");
    //首頁
    shiroFilterFactoryBean.setSuccessUrl("/index");
    //錯(cuò)誤頁面,認(rèn)證不通過跳轉(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ù)庫中的密碼
    String dbPassword = (String) info.getCredentials();
    //進(jìn)行密碼的比對
    boolean flag = BCrypt.checkpw(inPassword,dbPassword);
    return flag;
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

標(biāo)題名稱:Springboot整合Shiro的代碼實(shí)例
網(wǎng)頁URL:http://aaarwkj.com/article18/igoddp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、微信小程序、App設(shè)計(jì)關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈、定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司
日韩网激情视频在线观看| 成人一区二区三区乱码| 天堂在线av免费观看| 亚洲欧美日韩颜射极品| 亚洲精品一区二区激情| 国语自产拍在线观看不卡| 国产伦精品二区三区视频| 国产亚洲欧美精品在线观看| 日韩一区欧美中文字幕| 国产精品夫妇在线激情啪| 性生活免费在线观看视频| 一区二区三区成人高清视频| 精品国产乱码一区二区三区| 久久亚洲天堂av丁香| 国产欧美日韩经典一区| 欧美激情另类综合国内| 最新日韩精品电影在线网| 一区三区精品久久久精品| 日韩欧美国产成人在线观看| 欧美aⅴ一区二区三区| 中文字幕一区二区中文字幕| 91久久高清国语自产拍| 午夜激情毛片在线观看| 精品人妻一区二区三区| 国产男女视频免费观看| 免费精品黑人一区二区三区| 国产亚洲欧美日韩精品| 日本黄色录像在线观看| 好色人妻在线播放中文字幕| 欧洲女人av天堂精品| 精品国产无遮挡污污网站| 这里只有精品国产999| 男女性生活视频成年人观看| 成人黄性视频免费网看| 日韩天堂视频在线播放| 国产免费成人在线视频| 中文字幕日韩精品国产| 亚欧乱色熟女一区二区三区| 高清免费在线自偷自拍| 九九热视频这里是精品| 精品一区精品二区国产日韩|