`
dean_liu
  • 浏览: 75317 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring Security国际化

    博客分类:
  • Java
 
阅读更多
Spring Security版本:2.0.5

取出Spring Security的异常信息
${sessionScope['SPRING_SECURITY_LAST_EXCEPTION'].message}

异常信息已经经过国际化处理,所以只要直接取出异常的message属性即可。
如在org.springframework.security.providers.dao.AbstractUserDetailsAuthenticationProvider.authenticate(Authentication)中:
...
            try {
                user = retrieveUser(username,(UsernamePasswordAuthenticationToken) authentication);
            } catch (UsernameNotFoundException notFound) {
                if (hideUserNotFoundExceptions) {
                    throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
                } else {
                    throw notFound;
                }
            }
...


messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials")


org.springframework.context.support.MessageSourceAccessor
	protected Locale getDefaultLocale() {
		return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale());
	}

	public String getMessage(String code, String defaultMessage) {
		return this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale());
	}


org.springframework.context.i18n.LocaleContextHolder
	public static Locale getLocale() {
		LocaleContext localeContext = getLocaleContext();
		return (localeContext != null ? localeContext.getLocale() : Locale.getDefault());
	}

	public static LocaleContext getLocaleContext() {
		LocaleContext localeContext = (LocaleContext) localeContextHolder.get();
		if (localeContext == null) {
			localeContext = (LocaleContext) inheritableLocaleContextHolder.get();
		}
		return localeContext;
	}


可知保存Local信息的localeContext是从线程变量localeContextHolder中取出来的,从而可以知道必须先在localeContextHolder中设置好localeContext,国际化才能正确工作。

Spring框架中有一个过滤器是负责这个工作的,它就是org.springframework.web.filter.RequestContextFilter。

在web.xml中定义RequestContextFilter,注意要定义在springSecurityFilterChain前面才能正确工作。

	<filter>
	    <filter-name>localizationFilter</filter-name>
	    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
	</filter>
	
	<filter-mapping>
	    <filter-name>localizationFilter</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>


/**
 * Servlet 2.3 Filter that exposes the request to the current thread,
 * through both {@link org.springframework.context.i18n.LocaleContextHolder} and
 * {@link RequestContextHolder}. To be registered as filter in <code>web.xml</code>.
 *
 * <p>Alternatively, Spring's {@link org.springframework.web.context.request.RequestContextListener}
 * and Spring's {@link org.springframework.web.servlet.DispatcherServlet} also expose
 * the same request context to the current thread.
 *
 * <p>This filter is mainly for use with third-party servlets, e.g. the JSF FacesServlet.
 * Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.
 *
 * @author Juergen Hoeller
 * @author Rod Johnson
 * @since 2.0
 * @see org.springframework.context.i18n.LocaleContextHolder
 * @see org.springframework.web.context.request.RequestContextHolder
 * @see org.springframework.web.context.request.RequestContextListener
 * @see org.springframework.web.servlet.DispatcherServlet
 */

由RequestContextFilter的注释可知它主要用于third-party servlets,Spring MVC正常是不需要使用的,由此可知Spring Security与Spring MVC配合得不太完美。

参考链接:
http://stackoverflow.com/questions/6572377/spring-security-with-acceptheaderlocaleresolver-and-i18n
分享到:
评论

相关推荐

    spring security国际化及UserCache的配置和使用

    主要介绍下国际化的配置及UserCache的配置及使用教程,感兴趣的朋友参考下实现代码吧

    微信扫一扫登录、微信支付、springsecurity&oauth2

    项目中使用到的技术包含SpringBoot、SpringSecurity&oauth2(安全资源和授权中心模式、包括登录接口自定义返回字段、自定义手机号+密码登录、自定义免密登录)、Queue队列、线程池、xss攻击配置、SpringCache、Mybatis...

    Spring Security 中文教程.pdf

    5.6. 国际化 6. 核心服务 6.1. The AuthenticationManager , ProviderManager 和 AuthenticationProvider s 6.1.1. DaoAuthenticationProvider 6.2. UserDetailsService 实现 6.2.1. 内存认证 6.2.2. ...

    SpringSecurity 3.0.1.RELEASE.CHM

    5.6. 国际化 6. 核心服务 6.1. The AuthenticationManager, ProviderManager 和 AuthenticationProviders 6.1.1. DaoAuthenticationProvider 6.2. UserDetailsService实现 6.2.1. 内存认证 6.2.2. JdbcDaoImpl...

    Spring Security-3.0.1中文官方文档(翻译版)

    5.6. 国际化 6. 核心服务 6.1. The AuthenticationManager , ProviderManager 和AuthenticationProvider s 6.1.1. DaoAuthenticationProvider 6.2. UserDetailsService 实现 6.2.1. 内存认证 6.2.2. ...

    4、Spring Security 安全权限管理手册

    1、区分Authentication(验证)与 Authorization(授权) 验证 这个用户是谁? 用户身份可靠吗? 授权 某用户A是否可以访问资源R 某用户A是否可以执行M操作 某用户A是否可以对资源R执行...11、本地化消息输出(国际化)

    Spring_Security_3权限管理

    本文档内容为基于Spring下的权限管理,主要包含以下内容1、区分Authentication(验证)与 Authorization(授权)2、SS中的验证特点3、SS中的授权特点4、SS核心安全实现5、配置SS6、配置...11、本地化消息输出(国际化)

    blank-spring-noxml:没有任何 XML 的 Spring 应用程序(Spring MVC、Spring Security、国际化、JPA、Hibernate、WebJars)

    没有任何 XML 的 Spring 应用程序(Spring MVC、Spring Security、国际化、JPA、Hibernate、WebJars) MySQL(可在以下位置更改:src/main/resources/persistence.properties) 名称:blank-spring-noxml 用户:...

    spring-boot示例项目

    template-thymeleaf|[thymeleaf实现应用国际化示例](https://github.com/smltq/spring-boot-demo/blob/master/template-thymeleaf) mq-redis|[redis之mq实现,发布订阅模式]...

    boot-login-service:基于SpringBoot、SpringSecurity的登录功能。包含验证码、踢出用户、国际化、全局异常、

    BootLoginService 本项目为基于Spring-Boot、Spring-Security的登录功能 作者邮箱: Spring Boot 用户登录功能实现session共享,踢出用户,锁定用户等。

    Spring攻略(第二版 中文高清版).part1

    11.7 国际化(I18n)信息属性 458 11.7.1 问题 458 11.7.2 解决方案 458 11.7.3 工作原理 458 11.8 改变永久性存储系统 461 11.8.1 问题 461 11.8.2 解决方案 461 11.4.3 工作原理 461 11.9 日志 ...

    Spring攻略(第二版 中文高清版).part2

    11.7 国际化(I18n)信息属性 458 11.7.1 问题 458 11.7.2 解决方案 458 11.7.3 工作原理 458 11.8 改变永久性存储系统 461 11.8.1 问题 461 11.8.2 解决方案 461 11.4.3 工作原理 461 11.9 日志 ...

    本科毕业设计,基于Java-web开发的内容管理系统(java cms),使用SpringBoot、vue、MyBatis等技术

    本科毕业设计,Java内容管理系统(java cms)。 前台模板 网站前台模板位于/src/main/webapp/templates目录,使用Freemarker技术。通过修改模板文件,可以完全控制...VueI18n: Vue 国际化组件。 Tinymce: 富文本编辑器。

    Spring-Boot-Blog

    MogonDB file 已经在我的仓库中贴了出来前端使用Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。使用Bootstrap作为一个web框架,JQuery...

    若依前后端分离项目部署文档(完整版)

    后端采用Spring Boot、Spring Security、Redis & Jwt。 权限认证使用Jwt,支持多终端认证系统。 支持加载动态权限菜单,多方式轻松权限控制。 高效率开发,使用代码生成器可以一键生成前后端代码。)

    SecurityProject.rar

    spring-security 是一个独立的权限控制机制,使用简单 而且功能强大, 该Demo中有最基本的 登陆认证, session管理,国际化管理,权限控制细粒度到服务层的 方法级别 等 你所需要的基本东西都能满足

    最新ssm项目大湾区旅游推荐系统的设计与实现+vue.zip

    6. **多语言支持**:考虑到大湾区的国际化特点,系统支持多语言界面,满足不同背景用户的需求。 7. **安全性设计**:后端采用Spring Security进行安全控制,确保了用户认证和授权的安全性,保护用户数据不被未授权...

    library-app:BestLib库系统

    该应用程序提供基本的CRUD操作以及国际化和帐户注册过程。 它由Spring Security保护,并连接到外部MySQL数据库。 这是我的第一个“真实”应用程序,是第一次编程经验。技术领域使用以下项目创建项目: Java 11 ...

    legendshop2.1源码

    legendShop是目前最完善的Java商城之...9、支持国际化功能,支持多国语言,适合做外贸性生意。 10、支持全文搜索,用户可以查询所有的商城的商品信息。 11、支持多个地域协同销售,是个类似淘宝商城的微型商城系统。

    app-template:Web 应用开发的模板,后端 SpringBoot,前端 vue(Web app development template. Backend is SpringBoot. Frontend is vue)

    开发环境与生成环境的配置切换用户登录登出,并使用 spring boot + jwt 进行授权认证用户权限控制项目运行日志的在线查看与下载项目日志输出级别的动态切换客户端日志的上报、查看与下载前后端国际化处理Swagger ...

Global site tag (gtag.js) - Google Analytics