关于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. How to install the NVIDIA drivers on Fedora 32

    https://linuxconfig.org/how-to-install-the-nvidia-drivers-on-fedora-32 The NVIDIA Driver is a progra ...

  2. 微信小程序 audio组件 默认控件 无法隐藏/一直显示/改了controls=‘false’也没用2019/5/28

    <audio>默认控件,如果需要隐藏,不需要特意设置controls = 'false',直接把这个属性删除即可,不然无论如何都会存在 之前,设置了controls = 'false' & ...

  3. golang xpath解析网页

    https://github.com/antchfx/htmlquery package main import ( "fmt" "github.com/antchfx/ ...

  4. rabbitmq 延时队列

    前言 某个产品 或者订单,有个有效期 过了有效期要取消 方法一 : 写个脚本,用crontab 定时扫描 改变状态 但是最低只能一分钟 ,不适合 方法二 : 用swoole得毫秒定时器,每秒钟去扫描表 ...

  5. 往with as中写入数据的方法

    方法1:直接写入,使用union all,简单直观,但程序运行效率低,几百条就很慢了 with dw_wms_outbound_info_v100 as( select '10700001' as o ...

  6. 【转】Python3 如何优雅地使用正则表达式(完整版)

    转载自鱼c论坛 :  https://fishc.com.cn/thread-57073-1-1.html 注:本文翻译自 Regular Expression HOWTO,小甲鱼童鞋对此做了一些注释 ...

  7. linux环境下protobuf安装

    1. 到GitHub下载源码,执行解压命令后,进入解压后的目录 2. 执行./autogen,生成configure 3. 执行./configure --prefix=/usr/local/,pro ...

  8. 解放开发者!3款工具实现快速K8S开发

    本文转自Rancher Labs 关注我们,即可第一时间获取K8S教程哦 简 介 时至今日,Kubernetes正在变得越来越重要,不仅仅是运维需要Kubernetes,在开发的世界里Kubernet ...

  9. python中的多(liu)元(mang)交换 ,赋值

    多元赋值 顾名思义 同时对多个变量赋值 长话短说 举例: int x = 1 int y = 2 x,y = y ,x 这种写法可以直接交换x,y的值 非常方(liu)便(mang) 也就是 y=1 ...

  10. gulp + angularjs

    示例项目介绍 文中使用的例子是一个基于 Angular.js 实现的网页版 Todo App,在 Github 中下载angular-quickstart.项目代码结构如下 清单 5. 项目目录结构 ...