关于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. javascript 原型与原型链浅析

    原型 和原型链 什么是原型链 简单理解就是原型组成的链,对象的__proto__它的是原型,而原型也是一个对象,也有__proto__属性,原型的__proto__又是原型的原型,就这样可以一直通过_ ...

  2. Docker学习:(一)初识Docker

    Docker(容器虚拟化技术)要点(秒级启动) Docker的WWH公式学习 What[是什么]. Why[为什么要用它]. How[怎么用] 1.Docker简介 (1)问题:为什么会有docker ...

  3. 多测师讲解自动化测试 _如何解决验证码的问题_高级讲师肖sir

    自动化测试如何解决验证码的问题对于web应用来说,大部分的系统在用户登录时都要求用户输入验证码,验证码的类型的很多,有字母数字的,有汉字的,甚至还要用户输入一条算术题的答案的,对于系统来说使用验证码可 ...

  4. 如何设计一个牛逼的API接口

    在日常开发中,总会接触到各种接口.前后端数据传输接口,第三方业务平台接口.一个平台的前后端数据传输接口一般都会在内网环境下通信,而且会使用安全框架,所以安全性可以得到很好的保护.这篇文章重点讨论一下提 ...

  5. linux(centos8):kubeadm单机安装kubernetes(kubernetes 1.18.3)

    一,kubernetes安装的准备工作: 1,准备工作的各步骤请参见: https://www.cnblogs.com/architectforest/p/13141743.html   2,  ma ...

  6. centos8平台:redis6配置启用io多线程(redis6.0.1)

    一,linux平台上redis6的安装 请参见这一篇: https://www.cnblogs.com/architectforest/p/12830056.html 说明:刘宏缔的架构森林是一个专注 ...

  7. 爬虫学习之-scrapy交互式命令 scrapy查看页面

    scrapy shell https:///www.baidu.com  会启动爬虫请求网页 view(response) 会在浏览器打开请求到的临时文件 response.xpath("/ ...

  8. 国内首个 .NET 5 框架 Fur 斩获 1000 stars,1.0.0-rc.final.20 发布

          Fur 是 .NET 5 平台下企业应用开发最佳实践框架. 通往牛逼的路上,风景差得让人只想说脏话,但我在意的是远方. 啥环境 早在 1998 年微软公司对外发布 .NET/C# 平台的那 ...

  9. css自定义字体----使用外部字体文件

    css外部自定义字体 给大家分享一个使用的css小技巧!记得收藏呀!相信大家在浏览各种网站会见到各种奇形怪状花里胡哨的文字,还有就是一些浏览器兼容性问题,不会支持一些特殊的字体!给大家分享一个极其简单 ...

  10. SpringBoot+HikariCP+Dropwizard-Metrics统计连接池使用情况

    SpringBoot+HikariCP+Dropwizard-Metrics统计连接池使用情况 背景,HikariCP是Java目前使用最广的连接池工具类,SpringBoot默认也是用这个,现在想获 ...