我的新书:《亿级流量网站架构核心技术》
示例工程是Maven工程,需要了解Maven基础。
跟我学Shiro(我的公众号:kaitao-1234567,我的新书:《亿级流量网站架构核心技术》)
示例工程是Maven工程,需要了解Maven基础。
<!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
在Ehcache version2.5之后将会出现以下异常
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
- Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
- Shutdown the earlier cacheManager before creating new one with same name.
建议修正为以下配置
<!-- 缓存管理器 使用Ehcache实现 -->
<bean id="cache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="shared" value="true" />
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"
init-method="init" destroy-method="destroy">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
<property name="cacheManager" ref="cache" />
</bean>
张老师你好,我在配置了shiro之后,原先的swagger不显实接口信息了,搜寻资料未果,特此向您请教。以下是我的shiro过滤配置
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
System.out.println("\nShiroConfiguration.shirFilter开始\n");
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager());
//拦截器
Map<String, String> filters = new LinkedHashMap<String, String>();
filters.put("/swagger-ui.html","anon");
filters.put("/swagger/**","anon");
filters.put("/webjars/**", "anon");
filters.put("/swagger-resources/**","anon");
filters.put("/v2/**","anon");
filters.put("/doc.html","anon");
//配置不会被拦截的链接,顺序判断 anon所有url都可以匿名访问
// filters.put("/**","user");//记住我之后,所有url都可以访问,这显然在实际开发中是不可取的
filters.put("/static/**", "anon");
filters.put("/sys/selectAll","roles[超级管-理员]");//如果是roles[超级管理员,管理员]用户要同时满足所有角色
filters.put("/sys/selectAll","roles[管理员]");
filters.put("/sys/findByAccount","perms[test3]");
//配置退出过滤器
filters.put("/user/logout","logout");
//过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了
//authc:所有url都必须认证通过才可以访问;
//如果不设置会自动寻找Web工程根目录下的/login.jsp页面
shiroFilterFactoryBean.setLoginUrl("/user/login");
shiroFilterFactoryBean.setSuccessUrl("/index");
//未授权页面
shiroFilterFactoryBean.setUnauthorizedUrl("/error/he");
filters.put("/**", "authc");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filters);
System.out.println("\nShiroConfiguration.shirFilter结束\n");
return shiroFilterFactoryBean;
}
期待您的回复。
很感谢你写了这么一篇教程,让入门者得益。我遇到这么一个问题,项目用了ehcache,作为管理员打算删除用户A的权限,这时候修改了数据库用户A的之后,需要删除用户A的AuthorizationInfo cache。但是管理员拿不到用户A的principal,我暂时用了这招来删除。
public class ShiroDbRealm extends AuthorizingRealm {
public void clearSomeoneCachedAuthorizationInfo(String username) {
//用户名肯定是唯一的
Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
Object k = null;
if (cache != null) {
for (Object key : cache.keys()) {
if (key.toString().equals(username)) {
k = key;
break;
}
}
cache.remove(k);
}
}
}
但是我觉得这种遍历的方法很差劲,其次也没有考虑到对cache加锁。你有更好的建议吗?或者shiro已经内置了更好的api可以处理这种需求。 望告知,谢谢
并发控制的同步实现,不知道该怎么弄
Non-resolvable parent POM for com.github.zhangkaitao:shiro-example-chapter23-server:[unknown-version]: Could not find artifact com.github.zhangkaitao:shiro-example-chapter23-pom:pom:1.0-SNAPSHOT and 'parent.relativePath' points at wrong local POM @ line 5, column 13 -> [Help 2]
自定义ModularRealmAuthenticator管理多个Realm,我看到大都是使用xml文件配置的,请问注解怎么配置?
/shiro-example-chapter3/src/test/java/com/github/zhangkaitao/shiro/chapter3/realm/myRealm.java
or
/shiro-example-chapter3/src/test/java/com/github/zhangkaitao/shiro/chapter3/realm/MyRealm.java
提交到这上面的代码也太不严谨了吧。。。。。。
难道您本地没有这个问题
把代码导入eclispe后,貌似这类的问题不是一个半个
第40行的if中应该是 == 1 不是>1
请问第五章的编码加密的PasswordTest类下的testPasswordServiceWithJdbcRealm测试为什么无法通过,会报如下错误:
org.apache.shiro.authc.IncorrectCredentialsException: Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken - wu, rememberMe=false] did not match the expected credentials.
这一句,
login("classpath:shiro-jdbc-passwordservice.ini", "wu", "123");
是通过下边的ini配置进行加密的吗?
jdbcRealm.credentialsMatcher=$passwordMatcher
passwordService=org.apache.shiro.authc.credential.DefaultPasswordService
passwordMatcher=org.apache.shiro.authc.credential.PasswordMatcher
passwordMatcher.passwordService=$passwordService
其中passwordService是否要重写默认的?
//检查提交的客户端id是否正确
if (!oAuthService.checkClientId(oauthRequest.getClientId())) {
OAuthResponse response =
OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
.setError(OAuthError.TokenResponse.INVALID_CLIENT)
.setErrorDescription(Constants.INVALID_CLIENT_DESCRIPTION)
.buildJSONMessage();
return new ResponseEntity(response.getBody(),
HttpStatus.valueOf(response.getResponseStatus()));
}
.buildJSONMessage();方法一直报错
登录成功后,点击浏览器的buck按钮,之后就出现无法登录的问题,大家遇到过没
分布式系统,应该使用缓存存储url规则,程序应用规则是应用到单独一台的,如何触发修改一台,然后更新全部?
首先您的跟我学shiro系列让鄙人很受益,减少了不少学习成本,谢谢!
在项目过程中发现一个问题,在ie浏览器下,直接关闭浏览器,再次登陆会显示上一个登陆账号的信息。原因是ie的进程其实是还在的,彻底杀掉进程就不会。我自己尝试了几种解决办法,最终选择了使用一个过滤器在shiro过滤器前,如果发现请求是login且是post提交就先做logout操作。但感觉这始终是没有办法的办法,所以想请教下您,是不是框架本身就提供了类似的功能,或者以你的经验有没有更好更优雅的方法?谢谢!
您好!开涛老师!什么时候能加进您的shiro交流群啊!!!急切的想向您学习shiro技术。。跪求把握加进您的交流群啊!qq:997478173
这句的作用是什么?
BeanUtilsBean.getInstance().getConvertUtils().register(new EnumConverter(), JdbcRealm.SaltStyle.class);
跟下面这句看不出有什么关连
jdbcRealm.saltStyle=COLUMN
我继承了casFilter,但是取不到ticket。不知道后续应该怎么去验证。(我的工程是springmvc+shrio+cas)
可以把shiro-ehcache添加进去,
反正配置也不多:
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager securityManager.cacheManager = $cacheManager
spring mvc + shiro的服务要多机部署,所以将session存放到redis中,但是发现自己实现SessionDao时,其中的doReadSession调用频繁,多达10次,这个问题要怎么处理比较好?
RolePermssion 应该是RolePermission
<bean id="springCacheManager"
<bean id="ehcacheManager"
<bean id="cacheManager"
Can you tell me why I always get that error ?
It tells that DelegateSubject is not serializable. I am using shiro v1.4.*
public static Session deserialize(String sessionStr) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
ObjectInputStream ois = new ObjectInputStream(bis);
return (Session)ois.readObject();
} catch (Exception e) {
throw new RuntimeException("deserialize session error", e);
}
}
lifecyclebeanpostprocessor负责管理shiro的生命周期,项目中不加lifecyclebeanpostprocessor也没问题,而它在什么场景要用呢?
While i'm browsing Shrio documentation https://shiro.apache.org/spring.html, i found note don't use (MethodInvokingFactoryBean) bean, for web application, but you have use it with all web apps example,
is there any best practices for that?
spring expression xx.jar,spring aop xx.jar,srping web-mvc xx.jar几个在build path中看到是报错的
在咱这个demo里没有找到jdbcrealm,麻烦作者指导一下.
参考第十二章的时候,项目运行出现了异常:
org.springframework.beans.MethodInvocationException: Property 'cacheManager' threw exception; nested exception is org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Caches cannot be added by name when default cache config is not specified in the config. Please add a default cache config in the configuration.
不知道是不是版本的问题。ehcache官方文档2.4版的说要在配置文件中加上一个 defaultcache :
http://ehcache.org/documentation/faq#what-does-the-following-error-mean-caches-cannot-be-added-by-name-when-default-cache-config-is-not-specified-in-the-config-please-add-a-default-cache-config-in-the-configuration
RT
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.