激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring boot集成shiro詳細教程(小結)

spring boot集成shiro詳細教程(小結)

2021-03-15 12:31bweird Java教程

這篇文章主要介紹了spring boot 集成shiro詳細教程,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

我們開發時候有時候要把傳統spring shiro轉成spring boot項目,或者直接集成,name我們要搞清楚一個知識,就是 xml配置和spring bean代碼配置的關系,這一點很重要,因為spring boot是沒有xml配置文件的(也不絕對,spring boot也是可以引用xml配置的)

引入依賴:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<dependency>
 <artifactId>ehcache-core</artifactId>
 <groupId>net.sf.ehcache</groupId>
 <version>2.5.0</version>
</dependency>
<dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-ehcache</artifactId>
 <version>1.2.2</version>
</dependency>
 
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-api</artifactId>
   <version>1.7.25</version>
</dependency
<dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-core</artifactId>
 <version>1.2.3</version>
</dependency>
<dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-web</artifactId>
 <version>1.2.3</version>
</dependency>
<dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-spring</artifactId>
 <version>1.2.3</version>
</dependency>

如果你出現錯誤 找不到slf4j,引入依賴

?
1
2
3
4
5
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.7.25</version>
</dependency>

傳統xml配置如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx
 <a href="http://www.springframework.org/schema/tx/spring-tx.xsd">" rel="external nofollow">http://www.springframework.org/schema/tx/spring-tx.xsd">
</a> <context:component-scan base-package="com.len"/>
 <!--定義realm-->
 <bean id="myRealm" class="com.len.core.shiro.LoginRealm">
  <property name="credentialsMatcher" ref="credentialsMatcher"/>
 </bean>
 <bean id="permissionFilter" class="com.len.core.filter.PermissionFilter"/>
 <!--目前去掉自定義攔截驗證 由個人處理登錄業務-->
 <!--<bean id="customAdvicFilter" class="com.len.core.filter.CustomAdvicFilter">
  <property name="failureKeyAttribute" value="shiroLoginFailure"/>
 </bean>-->
 <bean id="verfityCodeFilter" class="com.len.core.filter.VerfityCodeFilter">
  <property name="failureKeyAttribute" value="shiroLoginFailure"/>
  <property name="jcaptchaParam" value="code"/>
  <property name="verfitiCode" value="true"/>
 </bean>
 <!-- Shiro過濾器 -->
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  <property name="securityManager" ref="securityManager"/>
 <property name="loginUrl" value="/login"/>
  <property name="unauthorizedUrl" value="/goLogin" />
  <property name="filters">
   <map>
    <entry key="per" value-ref="permissionFilter"/>
    <entry key="verCode" value-ref="verfityCodeFilter"/>
    <!--<entry key="main" value-ref="customAdvicFilter"/>-->
   </map>
  </property>
  <property name="filterChainDefinitions">
   <value>
    <!-- /** = anon所有url都可以匿名訪問 -->
    /login = verCode,anon
    /getCode = anon
    /logout = logout
    /plugin/** = anon
    <!-- /** = authc 所有url都必須認證通過才可以訪問-->
    /user/**=per
    <!-- /login = main-->
    /** = authc
   </value>
  </property>
 </bean>
 
 <!--安全管理器-->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  <property name="realm" ref="myRealm"/>
  <property name="cacheManager" ref="cacheManager" />
  <!--<property name="rememberMeManager" ref="rememberMeManager" />-->
 </bean>
 
 <!-- 憑證匹配器 -->
 <bean id="credentialsMatcher"
  class="com.len.core.shiro.RetryLimitCredentialsMatcher">
  <constructor-arg index="0" ref="cacheManager"/>
  <constructor-arg index="1" value="5"/>
  <property name="hashAlgorithmName" value="md5"/>
  <property name="hashIterations" value="4"/>
 </bean>
 <!--緩存-->
 <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml"/>
 </bean>
 
 
 <!--開啟注解-->
 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  <property name="securityManager" ref="securityManager" />
 </bean>
 
 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

轉換成bean 新建類ShiroConfig

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@Configuration
public class ShiroConfig {
 @Bean
 public RetryLimitCredentialsMatcher getRetryLimitCredentialsMatcher(){
  RetryLimitCredentialsMatcher rm=new RetryLimitCredentialsMatcher(getCacheManager(),"5");
  rm.setHashAlgorithmName("md5");
  rm.setHashIterations(4);
  return rm;
 
 }
 @Bean
 public LoginRealm getLoginRealm(){
  LoginRealm realm= new LoginRealm();
  realm.setCredentialsMatcher(getRetryLimitCredentialsMatcher());
  return realm;
 }
 
 @Bean
 public EhCacheManager getCacheManager(){
  EhCacheManager ehCacheManager=new EhCacheManager();
  ehCacheManager.setCacheManagerConfigFile("classpath:ehcache/ehcache.xml");
  return ehCacheManager;
 }
 
 @Bean
 public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
  return new LifecycleBeanPostProcessor();
 }
 
 @Bean(name="securityManager")
 public DefaultWebSecurityManager getDefaultWebSecurityManager(){
  DefaultWebSecurityManager dwm=new DefaultWebSecurityManager();
  dwm.setRealm(getLoginRealm());
  dwm.setCacheManager(getCacheManager());
  return dwm;
 }
 
 @Bean
 public PermissionFilter getPermissionFilter(){
  PermissionFilter pf=new PermissionFilter();
  return pf;
 }
 
 @Bean
 public VerfityCodeFilter getVerfityCodeFilter(){
  VerfityCodeFilter vf= new VerfityCodeFilter();
  vf.setFailureKeyAttribute("shiroLoginFailure");
  vf.setJcaptchaParam("code");
  vf.setVerfitiCode(true);
  return vf;
 }
 
 @Bean(name = "shiroFilter")
 public ShiroFilterFactoryBean getShiroFilterFactoryBean(){
  ShiroFilterFactoryBean sfb = new ShiroFilterFactoryBean();
  sfb.setSecurityManager(getDefaultWebSecurityManager());
  sfb.setLoginUrl("/login");
  sfb.setUnauthorizedUrl("/goLogin");
  Map<String, Filter> filters=new HashMap<>();
  filters.put("per",getPermissionFilter());
  filters.put("verCode",getVerfityCodeFilter());
  sfb.setFilters(filters);
  Map<String, String> filterMap = new LinkedHashMap<>();
  filterMap.put("/login","verCode,anon");
  //filterMap.put("/login","anon");
  filterMap.put("/getCode","anon");
  filterMap.put("/logout","logout");
  filterMap.put("/plugin/**","anon");
  filterMap.put("/user/**","per");
  filterMap.put("/**","authc");
  sfb.setFilterChainDefinitionMap(filterMap);
  return sfb;
 }
 
 @Bean
 public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
  DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
  advisorAutoProxyCreator.setProxyTargetClass(true);
  return advisorAutoProxyCreator;
 }
 
 @Bean
 public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(){
  AuthorizationAttributeSourceAdvisor as=new AuthorizationAttributeSourceAdvisor();
  as.setSecurityManager(getDefaultWebSecurityManager());
  return as;
 }
 
 @Bean
 public FilterRegistrationBean delegatingFilterProxy(){
  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  DelegatingFilterProxy proxy = new DelegatingFilterProxy();
  proxy.setTargetFilterLifecycle(true);
  proxy.setTargetBeanName("shiroFilter");
  filterRegistrationBean.setFilter(proxy);
  return filterRegistrationBean;
 }

其中有個別類是自定義的攔截器和 realm,此時spring 即能注入shiro 也就是 spring boot集成上了 shiro

如果你因為其他配置引起一些失敗,可以參考開源項目 lenos快速開發腳手架 spring boot版本,其中有對shiro的集成,可以用來學習

地址:https://gitee.com/bweird/lenosp

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

原文鏈接:https://my.oschina.net/u/3312115/blog/1600830

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 在线播放av网址 | 国产成年人在线观看 | 日本欧美一区二区三区视频麻豆 | 黄色免费视频网站 | 久久综合给合久久狠狠狠97色69 | 在线香蕉视频 | 青草久久网 | 91小视频在线观看免费版高清 | 欧美成人久久 | 国产女厕一区二区三区在线视 | 色淫网站免费视频 | 伊人二本二区 | 日本成人午夜视频 | 黄色高清免费 | 欧美成网 | 久久精品国产亚洲7777 | 国产午夜亚洲精品午夜鲁丝片 | 91精品国产99久久久久久红楼 | 大胆在线日本aⅴ免费视频 永久免费毛片 | 午夜小视频免费观看 | 九九热精品视频在线 | 色悠悠久久久久 | 色婷婷综合久久久中文一区二区 | 欧美亚洲黄色片 | 午夜精品区 | av成人在线电影 | 亚洲第一色婷婷 | 欧美成人一区二区三区电影 | 成人一级免费视频 | av电影在线观看网站 | av成人在线观看 | 久久福利精品 | 欧美人与牲禽动交精品一区 | 久久精品片 | 日韩精品久久久久久久电影99爱 | 亚洲电影在线观看高清免费 | 红杏网站永久免费视频入口 | 色域tv | 草久在线观看视频 | 欧美黄一区| 亚洲国产精品久久久久久久久 |