Spring Security教程(5)---- 国际化配置及UserCache
2023-09-11 14:21:33 时间
这一章是为了给后面的讲解打基础的,主要介绍下国际化的配置及UserCache的配置及使用
国际化配置
- <!-- 定义上下文返回的消息的国际化 -->
- <bean id="messageSource"
- class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
- <property name="basename"
- value="classpath:config/messages_zh_CN"/>
- </bean>
basename中配置的是消息文件的路径
在spring-security-core-3.2.0.M1.jar包中的org.springframework.security下可以找到国际化文件,可以直接拿来,这个类也可以用在项目中
- @Autowired
- private MessageSource messageSource;
这样就可以在类中引如MessageSource使用了,MessageSource提供了下面三个方法
- String getMessage(String code, Object[] args, String defaultMessage, Locale locale);
- String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException;
- String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
比如我们在property文件中定义了如下消息
- UserDetails.isLocked=用户已被锁定
- UserDetails.userNotFound=用户{0}不存在
然后使用getMessage方法
getMessage("UserDetails.isLocked",null,null) //用户已被锁定
getMessage("UserDetails.isLocked",new Object[]{“admin”},null) //用户admin不存在
UserCache配置,通过ecahe实现
- <!-- 启用用户的缓存功能 -->
- <bean id="userCache"
- class="org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache">
- <property name="cache" ref="userEhCache" />
- </bean>
- <bean id="userEhCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
- <property name="cacheName" value="userCache" />
- <property name="cacheManager" ref="cacheManager" />
- </bean>
- <bean id="cacheManager"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
ehcache.xml
- <cache
- name="userCache"
- maxElementsInMemory="100"
- eternal="false"
- timeToIdleSeconds="600"
- timeToLiveSeconds="3600"
- overflowToDisk="true"
- />
注入ecache
- @Autowired
- private UserCache userCache;
这样在程序中就可以通过
this.userCache.getUserFromCache(username);获取到缓存中的用户对象
用户对象为UserDetails类型
相关文章
- Spring 入门demo
- 浅谈Shiro框架在Spring Boot中的认证应用
- 8 -- 深入使用Spring -- 6... Spring的事务
- 8 -- 深入使用Spring -- 2... Spring的“零配置”支持
- 深入实践Spring Boot3.3.1 Thymeleaf配置
- Too many open files ( spring-cloud-gateway )
- Spring 的 Controller 是单例还是多例?怎么保证并发的安全
- Spring配置项<context:annotation-config/>说明
- spring batch(二):核心部分(1):配置Spring batch
- Spring Boot 配置中的敏感信息如何保护?
- Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?
- 《Spring攻略(第2版)》——1.13 继承Bean配置
- 【spring boot】FilterRegistrationBean介绍
- Spring Cloud 系列之 Eureka 实现服务注册与发现
- spring框架漏洞整理(Spring Boot Actuator相关漏洞)
- spring boot容器启动详解
- Spring自带配置方式链接数据库(没有src新建文件,没有c3p0)
- Spring之AOP注解配置
- Spring Cloud Config实现集群配置中心
- Spring 之 AOP
- Spring、Spring MVC、MyBatis整合文件配置详解
- spring-data-redis的事务操作深度解析--原来客户端库还可以攒够了事务命令再发?
- Spring Security(1)--- 权限控制基本功能实现