关于Spring Security的使用,之前也整理过一些笔记,但是在提示信息的时候,总感觉还缺点什么?不管是不是前后端分离,我们都希望在登录验证出现错误的时候,能够提示友好的中文信息。

在前后端不分离的情况下,是通过throw new RuntimeException("错误信息描述")来抛出异常信息的,前端通过接收到这个异常信息来进行提示;在密码校验时,是通过实现PasswordEncoder接口,来进行校验的,如果校验不通过,那么前台接收到的异常信息是Bad credentials,这是框架自带的异常信息,并不是我们想要的。

在给用户提示信息时,还是把提示信息做的精准、精确一点比较好;用户名不存在,就是应该提示用户名不存在;密码不正确,就应该提示密码输入错误;而不应该模棱两可的提示,用户名或密码错误~!

模棱两可的提示既然如此不友好,那么该怎么做才能提示正确的信息呢?这里只需两步,在不修改源代码的情况下。

第一步,建立中文提示配置文件

AbstractAccessDecisionManager.accessDenied=不允许访问

AbstractLdapAuthenticationProvider.emptyPassword=坏的凭证

AbstractSecurityInterceptor.authenticationNotFound=未在SecurityContext中查找到认证对象

AbstractUserDetailsAuthenticationProvider.badCredentials=密码输入错误~!

AbstractUserDetailsAuthenticationProvider.credentialsExpired=用户凭证已过期

AbstractUserDetailsAuthenticationProvider.disabled=用户已失效

AbstractUserDetailsAuthenticationProvider.expired=用户帐号已过期

AbstractUserDetailsAuthenticationProvider.locked=用户帐号已被锁定

AbstractUserDetailsAuthenticationProvider.onlySupports=仅仅支持UsernamePasswordAuthenticationToken

AccountStatusUserDetailsChecker.credentialsExpired=用户凭证已过期

AccountStatusUserDetailsChecker.disabled=用户已失效

AccountStatusUserDetailsChecker.expired=用户帐号已过期

AccountStatusUserDetailsChecker.locked=用户帐号已被锁定

AclEntryAfterInvocationProvider.noPermission=给定的Authentication对象({0})根本无权操控领域对象({1})

AnonymousAuthenticationProvider.incorrectKey=展示的AnonymousAuthenticationToken不含有预期的key

BindAuthenticator.badCredentials=坏的凭证

BindAuthenticator.emptyPassword=坏的凭证

CasAuthenticationProvider.incorrectKey=展示的CasAuthenticationToken不含有预期的key

CasAuthenticationProvider.noServiceTicket=未能够正确提供待验证的CAS服务票根

ConcurrentSessionControlAuthenticationStrategy.exceededAllowed=已经超过了当前主体({0})被允许的最大会话数量

DigestAuthenticationFilter.incorrectRealm=响应结果中的Realm名字({0})同系统指定的Realm名字({1})不吻合

DigestAuthenticationFilter.incorrectResponse=错误的响应结果

DigestAuthenticationFilter.missingAuth=遗漏了针对'auth' QOP的、必须给定的摘要取值; 接收到的头信息为{0}

DigestAuthenticationFilter.missingMandatory=遗漏了必须给定的摘要取值; 接收到的头信息为{0}

DigestAuthenticationFilter.nonceCompromised=Nonce令牌已经存在问题了,{0}

DigestAuthenticationFilter.nonceEncoding=Nonce未经过Base64编码; 相应的nonce取值为 {0}

DigestAuthenticationFilter.nonceExpired=Nonce已经过期/超时

DigestAuthenticationFilter.nonceNotNumeric=Nonce令牌的第1部分应该是数字,但结果却是{0}

DigestAuthenticationFilter.nonceNotTwoTokens=Nonce应该由两部分取值构成,但结果却是{0}

DigestAuthenticationFilter.usernameNotFound=用户名{0}未找到

JdbcDaoImpl.noAuthority=没有为用户{0}指定角色

JdbcDaoImpl.notFound=未找到用户{0}

LdapAuthenticationProvider.badCredentials=坏的凭证

LdapAuthenticationProvider.credentialsExpired=用户凭证已过期

LdapAuthenticationProvider.disabled=用户已失效

LdapAuthenticationProvider.expired=用户帐号已过期

LdapAuthenticationProvider.locked=用户帐号已被锁定

LdapAuthenticationProvider.emptyUsername=用户名不允许为空

LdapAuthenticationProvider.onlySupports=仅仅支持UsernamePasswordAuthenticationToken

PasswordComparisonAuthenticator.badCredentials=坏的凭证

PersistentTokenBasedRememberMeServices.cookieStolen=Invalid remember-me token (Series/token) mismatch. Implies previous cookie theft attack.

ProviderManager.providerNotFound=未查找到针对{0}的AuthenticationProvider

RememberMeAuthenticationProvider.incorrectKey=展示RememberMeAuthenticationToken不含有预期的key

RunAsImplAuthenticationProvider.incorrectKey=展示的RunAsUserToken不含有预期的key

SubjectDnX509PrincipalExtractor.noMatching=未在subjectDN: {0}中找到匹配的模式

SwitchUserFilter.noCurrentUser=不存在当前用户

SwitchUserFilter.noOriginalAuthentication=不能够查找到原先的已认证对象

第二步,建立bean,覆盖框架默认的提示信息配置文件;

package com.yzy.auth.config;

import org.springframework.context.MessageSource;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.util.Locale;

/**

  • description: MySecurityMessages

  • date: 2020/11/10 13:13

  • author: faner

    */

    @Configuration

    public class MySecurityMessages {

    /**

    • 自定义错误信息
    • @return

      */

      @Bean

      public MessageSource messageSource() {

      Locale.setDefault(Locale.CHINA);

      ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

      //中文提示信息配置文件

      messageSource.addBasenames("classpath:messages_zh_CN");

      return messageSource;

      }

      }

Spring Security验证,提示正确的信息的更多相关文章

  1. Spring Security验证流程剖析及自定义验证方法

    Spring Security的本质 Spring Security本质上是一连串的Filter, 然后又以一个独立的Filter的形式插入到Filter Chain里,其名为FilterChainP ...

  2. Spring Security(05)——异常信息本地化

    Spring Security支持将展现给终端用户看的异常信息本地化,这些信息包括认证失败.访问被拒绝等.而对于展现给开发者看的异常信息和日志信息(如配置错误)则是不能够进行本地化的,它们是以英文硬编 ...

  3. spring security LDAP获取用户信息

    很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...

  4. 关于Spring Security 3获取用户信息的问题

    标签: spring security 3标签获取用户信息 2013-01-05 10:40 5342人阅读 评论(0) 收藏 举报  分类: Spring(25) java(70) 前端(7)    ...

  5. Spring Security教程(二):通过数据库获得用户权限信息

    上一篇博客中,Spring Security教程(一):初识Spring Security,我把用户信息和权限信息放到了xml文件中,这是为了演示如何使用最小的配置就可以使用Spring Securi ...

  6. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  7. 【Spring Cloud & Alibaba 实战 | 总结篇】Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现微服务统一认证授权和鉴权

    一. 前言 hi,大家好~ 好久没更文了,期间主要致力于项目的功能升级和问题修复中,经过一年时间的打磨,[有来]终于迎来v2.0版本,相较于v1.x版本主要完善了OAuth2认证授权.鉴权的逻辑,结合 ...

  8. Spring Security 表单登录

    1. 简介 本文将重点介绍使用Spring Security登录. 本文将构建在之前简单的Spring MVC示例之上,因为这是设置Web应用程序和登录机制的必不可少的. 2. Maven 依赖 要将 ...

  9. SpringBoot集成Spring Security(4)——自定义表单登录

    通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ...

随机推荐

  1. React Ref 其实是这样的

    大家好,我是Mokou,好久没有冒泡了,最近一直在看研究算法和数据结构方面的东西,但是似乎很多前端不喜欢看这种东西,而且目前本人算法方面也很挫,就不献丑了. 当然了,最近也开始研究React了,这篇文 ...

  2. Springboot+Redis(发布订阅模式)跨多服务器实战

    一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...

  3. 在shell中截取心仪的字符串

    file=/dir1/dir2/dir3/my.file.txt ${file#*/} 去掉左边 ${file##*/} 去掉左边最后一个 ${file%/*} 去掉右边 ${file%%/*} 去掉 ...

  4. 【C语言/C++程序员编程】一小时做出来的数字雨(一颗开花的树)!

    相信大家看过许许多多的关于计算机黑客.骇客.人工智能.AI方面的电影,每当黑客入侵某个五角大楼,某个网站时,都会出现这样一副画面: 入侵 或者这样的: 数字雨 然后就轻而易举的成功入侵夺取管理员权限了 ...

  5. 【原创】xenomai内核解析--实时内存管理--xnheap

    目录 一. xenomai内存池管理 1.xnheap 2. xnpagemap 3. xnbucket 4. xnheap初始化 5. 内存块分配 5.1 小内存分配流程(<= 2*PAGE_ ...

  6. go内建方法 new和make区别

    package mainimport ( "fmt" "reflect")func main() { // make函数 //makeSlice() // 创建 ...

  7. Go语言中Goroutine与线程的区别

    1.什么是Goroutine? Goroutine是建立在线程之上的轻量级的抽象.它允许我们以非常低的代价在同一个地址空间中并行地执行多个函数或者方法.相比于线程,它的创建和销毁的代价要小很多,并且它 ...

  8. Linux运维学习第一周记

    1 当年白岳伴清游, 2 江石台空一苇浮. 3 缥渺临风闻郢曲, 4 殷勤歧路看吴钩. 老气横秋方知世间沧桑! 以前一直忙,没有时间沉浸下来学习,一直都是浮着. 至此大疫,给生命按下了暂停键. 踏踏实 ...

  9. 学习Python 能找到工作?1300+条招聘信息告诉你答案

    对于python这块有任何不懂的问题可以随时来问我,我对于学习方法,系统学习规划,还有学习效率这些知道一些,希望可以帮助大家少走弯路.当然也会送给大家一份系统性的python资料,文末附有爬虫项目实战 ...

  10. abstract关键字的说法

    含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必 ...