Spring Security教程(5)---- 国际化配置及UserCache

这一章是为了给后面的讲解打基础的,主要介绍下国际化的配置及UserCache的配置及使用

国际化配置

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!-- 定义上下文返回的消息的国际化 -->  
  2. <bean id="messageSource"  
  3.     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  4.     <property name="basename"  
  5.         value="classpath:config/messages_zh_CN"/>  
  6. </bean>  
basename中配置的是消息文件的路径

在spring-security-core-3.2.0.M1.jar包中的org.springframework.security下可以找到国际化文件,可以直接拿来,这个类也可以用在项目中

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Autowired  
  2. private MessageSource messageSource;  
这样就可以在类中引如MessageSource使用了,MessageSource提供了下面三个方法

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. String getMessage(String code, Object[] args, String defaultMessage, Locale locale);  
  2. String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException;  
  3. String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;  
比如我们在property文件中定义了如下消息

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. UserDetails.isLocked=用户已被锁定  
  2. UserDetails.userNotFound=用户{0}不存在  
然后使用getMessage方法
getMessage("UserDetails.isLocked",null,null)  //用户已被锁定

getMessage("UserDetails.isLocked",new Object[]{“admin”},null)  //用户admin不存在


UserCache配置,通过ecahe实现

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!-- 启用用户的缓存功能 -->  
  2. <bean id="userCache"  
  3.     class="org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache">  
  4.     <property name="cache" ref="userEhCache" />  
  5. </bean>  
  6.   
  7. <bean id="userEhCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
  8.     <property name="cacheName" value="userCache" />  
  9.     <property name="cacheManager" ref="cacheManager" />  
  10. </bean>  
  11.   
  12. <bean id="cacheManager"  
  13.     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />  

ehcache.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <cache  
  2.     name="userCache"  
  3.     maxElementsInMemory="100"  
  4.     eternal="false"  
  5.     timeToIdleSeconds="600"  
  6.     timeToLiveSeconds="3600"  
  7.     overflowToDisk="true"  
  8. />  

注入ecache

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Autowired  
  2. private UserCache userCache;  
这样在程序中就可以通过

this.userCache.getUserFromCache(username);获取到缓存中的用户对象

用户对象为UserDetails类型

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 是一个开源的安全框架,用于保护基于Spring框架构建的应用程序。MyBatis-Plus 是基于 MyBatis 的增强工具,用于简化 MyBatis 操作和提高开发效率。 将 Spring Security 与 MyBatis-Plus 整合主要包括以下几个步骤: 1. 首先,在 Spring Boot 项目的 pom.xml 文件中添加 Spring Security 和 MyBatis-Plus 的依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>latest version</version> </dependency> ``` 2. 接下来,在项目的配置文件 application.properties 或 application.yml 中配置数据库连接和 MyBatis-Plus 的相关配置。 3. 创建一个用户实体类和对应的 Mapper 接口,使用 MyBatis-Plus 提供的注解和方法完成数据访问操作。 4. 创建一个自定义的 UserDetailsService 实现类,用于从数据库中获取用户信息,实现 loadUserByUsername 方法。 5. 创建一个自定义的 PasswordEncoder 实现类,用于加密和校验用户密码。 6. 配置 Spring Security,创建一个继承自 WebSecurityConfigurerAdapter 的类,并重写 configure 方法,设置用户认证规则、登录配置和访问控制规则。 7. 在 Spring Boot 启动类上使用 @MapperScan 注解,扫描 Mapper 接口。 通过以上步骤,就可以实现 Spring Security 与 MyBatis-Plus 的整合。在实际应用中,还可以根据具体需求进行一些其他配置和扩展,例如添加角色权限控制、自定义登录页面和处理逻辑等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值