- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章spring boot 集成shiro的配置方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
spring boot提供了一个自带的认证框架,同时也提供自定义的javaconfig配置扩展,spring-sercurity同样也是优秀的框架,但是习惯了用apache shiro框架,而且原项目就是集成的shiro框架,到网上找了一下配置方式,没找到完全配置的方法,因此决定自己动手,丰衣足食! 。
要在spring boot上集成其他框架,首先要会spring javaconfig方法,利用此方法同样可以配置其他模块,废话少说,开始。。.
开始前需要导入maven依赖(shiro-web可选): 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
|
原shiro集成spring的配置拿出来,如下:
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
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default
-lazy-init=
"true"
>
<description>Shiro安全配置 来源于: http:
//shiro.apache.org/spring.html
</description>
<bean id=
"securityManager"
class
=
"org.apache.shiro.web.mgt.DefaultWebSecurityManager"
>
<!-- Single realm app. If you have multiple realms, use the
'realms'
property
instead. -->
<property name=
"realm"
ref=
"ShiroRealmImpl"
/>
<property name=
"cacheManager"
ref=
"shiroEhcacheManager"
/>
</bean>
<!-- Define the realm you want to use to connect to your back-end security
datasource: -->
<bean id=
"ShiroRealmImpl"
class
=
"com.wechatserver.web.services.system.impl.ShiroRealmImpl"
/>
<bean id=
"shiroFilter"
class
=
"org.apache.shiro.spring.web.ShiroFilterFactoryBean"
>
<property name=
"securityManager"
ref=
"securityManager"
/>
<property name=
"loginUrl"
value=
"/login"
/> <!-- 没有权限或者失败后跳转的页面 -->
<property name=
"successUrl"
value=
"/sa/index"
/>
<property name=
"filterChainDefinitions"
>
<!-- , roles[admin], perms[document:read] -->
<value>
<!--
/user/** = authc
/role/edit/* = perms[role:edit]
/role/save = perms [role:edit]
/role/list = perms [role:view]
-->
/sa/** = authc
/** = anon
</value>
</property>
</bean>
<!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
<bean id=
"shiroEhcacheManager"
class
=
"org.apache.shiro.cache.ehcache.EhCacheManager"
>
<property name=
"cacheManagerConfigFile"
value=
"classpath:ehcache-shiro.xml"
/>
</bean>
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id=
"lifecycleBeanPostProcessor"
class
=
"org.apache.shiro.spring.LifecycleBeanPostProcessor"
/>
<!-- AOP式方法级权限检查 -->
<!-- Enable Shiro Annotations
for
Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean
class
=
"org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on=
"lifecycleBeanPostProcessor"
>
<property name=
"proxyTargetClass"
value=
"true"
/>
</bean>
<bean
class
=
"org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"
>
<property name=
"securityManager"
ref=
"securityManager"
/>
</bean>
</beans>
|
好多类啊,没办法一个一个配置,javaconfig文件如下:
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
|
import
java.util.LinkedHashMap;
import
java.util.Map;
import
org.apache.shiro.cache.ehcache.EhCacheManager;
import
org.apache.shiro.spring.LifecycleBeanPostProcessor;
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.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
@Configuration
public
class
ShiroConfiguration {
private
static
Map<String, String> filterChainDefinitionMap =
new
LinkedHashMap<String, String>();
@Bean
(name =
"ShiroRealmImpl"
)
public
ShiroRealmImpl getShiroRealm() {
return
new
ShiroRealmImpl();
}
@Bean
(name =
"shiroEhcacheManager"
)
public
EhCacheManager getEhCacheManager() {
EhCacheManager em =
new
EhCacheManager();
em.setCacheManagerConfigFile(
"classpath:ehcache-shiro.xml"
);
return
em;
}
@Bean
(name =
"lifecycleBeanPostProcessor"
)
public
LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
return
new
LifecycleBeanPostProcessor();
}
@Bean
public
DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator daap =
new
DefaultAdvisorAutoProxyCreator();
daap.setProxyTargetClass(
true
);
return
daap;
}
@Bean
(name =
"securityManager"
)
public
DefaultWebSecurityManager getDefaultWebSecurityManager() {
DefaultWebSecurityManager dwsm =
new
DefaultWebSecurityManager();
dwsm.setRealm(getShiroRealm());
dwsm.setCacheManager(getEhCacheManager());
return
dwsm;
}
@Bean
public
AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {
AuthorizationAttributeSourceAdvisor aasa =
new
AuthorizationAttributeSourceAdvisor();
aasa.setSecurityManager(getDefaultWebSecurityManager());
return
new
AuthorizationAttributeSourceAdvisor();
}
@Bean
(name =
"shiroFilter"
)
public
ShiroFilterFactoryBean getShiroFilterFactoryBean() {
ShiroFilterFactoryBean shiroFilterFactoryBean =
new
ShiroFilterFactoryBean();
shiroFilterFactoryBean
.setSecurityManager(getDefaultWebSecurityManager());
shiroFilterFactoryBean.setLoginUrl(
"/login"
);
shiroFilterFactoryBean.setSuccessUrl(
"/sa/index"
);
filterChainDefinitionMap.put(
"/sa/**"
,
"authc"
);
filterChainDefinitionMap.put(
"/**"
,
"anon"
);
shiroFilterFactoryBean
.setFilterChainDefinitionMap(filterChainDefinitionMap);
return
shiroFilterFactoryBean;
}
}
|
注意点:最后一个是filterChainDefinitionMap的初始化,Map用的是LinkedHashMap来初始化的,各位应用的时候将其配置成properties文件,然后初始化就ok了,改写好后直接启动Ok,搬运到spring boot应该是OK的.
别忘了在ehcache-shiro.xml 。
1
2
3
4
5
6
7
8
9
10
11
|
<ehcache updateCheck=
"false"
name=
"shiroCache"
>
<defaultCache
maxElementsInMemory=
"10000"
eternal=
"false"
timeToIdleSeconds=
"120"
timeToLiveSeconds=
"120"
overflowToDisk=
"false"
diskPersistent=
"false"
diskExpiryThreadIntervalSeconds=
"120"
/>
</ehcache>
|
备注:ShiroRealmImpl类请参考官方文档 。
总结 。
以上所述是小编给大家介绍的spring boot 集成shiro的配置方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:http://blog.csdn.net/qiuqiupeng/article/details/38984779 。
最后此篇关于spring boot 集成shiro的配置方法的文章就讲到这里了,如果你想了解更多关于spring boot 集成shiro的配置方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我已经一遍又一遍地讨论如何让用户登录 Shiro,但似乎仍然缺少一个重要的部分:shiro 如何根据存储的用户名和密码验证给定的用户名和密码?我想到的最多的是 It is each Realm's r
Apache Shiro 文档暗示了一些用于捕获连续失败登录尝试(以及其他)的所需功能,但是,我找不到具体的文档。目前我可以执行 currentUser.login(token);使用无效密码无限次并
我想在下一个Web项目中使用Shiro,但是我不知道一种好的(如果不是最好的)策略来管理用户(shiro.ini中的[users])。 最好为每个注册成员创建Shiro用户吗? 还是创建一个Shiro
我有一个简单的网络项目。我想在这个项目中访问多个角色是一个 URL。 网址的 sihor.ini 部分 [urls] /login.xhtml = authc /logout = logout /ad
我正在使用 apache shiro。当我想知道用户是否有权限和角色时,我使用 SecutiryUtils.getSubject()。我想知道如何向主题添加更多信息,例如电子邮件、主键和我需要的任何其
1、Shiro认证过程 1、收集实体/凭据信息 复制代码 代码如下: //Example using most common scenario of usern
我正在尝试使用 Shiro 对 Tomcat 6 中运行的 servlet 进行身份验证。 我有以下 shiro.ini 文件: [main] ps = org.apache.shiro.authc.
Apache Shiro的配置主要分为四部分: 对象和属性的定义与配置 URL的过滤器配置 静态用户配置 静态角色配置 其中,由于用户、角色一般由后台进行操作的动态数据,因此Shiro配置一般仅包
我正在使用 Spring MVC、Tiles 和 Shiro。 这是我的未授权Url 属性的配置方式: 我的期望是,当 MyAuthorizingRealm发现无效凭据,Shiro 将重定向到 /un
我正在使用 Apache Shiro 开发基于 EJB 的网络服务来管理用户访问权限。我添加了 freshly released我的 Maven 项目的 Apache Shiro 1.5.0 版使用新
我正在将现有应用程序从Grails 2.4.4升级到Grails 3.2.8。我正在尝试从grails shiro插件迁移到grails spring-security-shiro插件。除了访问已登录
我正在将我的应用程序从grails 2.4.4迁移到grails 3.2.9。 我正在尝试迁移到 compile 'org.grails.plugins:spring-security-shiro:3
在我包含 shiro-core-1.2.2 和 shiro-web-1.2.2 之前,我的 Maven Web 项目工作正常但是当我在 pom.xml 中包含此依赖项之后,我在执行时收到错误mvn t
我将 Apache Shiro 与 Spring 一起使用,并且仅使用 Spring 的 Java 配置。 (没有 XML)。下面是我的配置类的一部分: @Configuration public c
我能够使用 shiro.ini 和 spring 运行 shiro,但我想使用 shiro 注释,所以我尝试在没有 ini 文件的情况下使用 shiro-spring。但这让我很难受错误: org.a
1. 权限管理 1.1 什么是权限管理? 权限管理实现对用户访问系统的控制,按照安全规则或者安全策略,可以控制用户只能访问自己被授权的资源 权限管理包括用户身份认证和授权两部分,简称认证授权 1.2
我们经常会有用到,当A 用户在北京登录 ,然后A用户在天津再登录 ,要踢出北京登录的状态。如果用户在北京重新登录,那么又要踢出天津的用户,这样反复。 这样保证了一个帐号只能同时一个人使用。那么下面
有没有办法在 Shiro 中实现多因素身份验证?有人可以给我一个关于如何实现这一点的提示吗? 更多细节: 基本思想是,用户需要像往常一样使用用户名和密码登录,但在实际验证之前,用户还需要输入他作为 S
ini 文件就像 [main] authc.loginUrl = /login.html authc.successUrl = /index.html authc.usernameParam = j_
我了解 spring jsf 和 hibernate。我已经整合了它们并创建了我自己的框架。现在我想使用 Apache Shiro 将 session 管理添加到我的框架中。但我对 Apache Sh
我是一名优秀的程序员,十分优秀!