【SpringSecurity+JWT单点登录,跳过密码校验】

DaoAuthenticationProvider

DaoAuthenticationProvider

DaoAuthenticationProvider中的additionalAuthenticationChecks方法负责比对密码

```java
@SuppressWarnings("deprecation")
	protected void additionalAuthenticationChecks(UserDetails userDetails,
			UsernamePasswordAuthenticationToken authentication)
			throws AuthenticationException {
		if (authentication.getCredentials() == null) {
			logger.debug("Authentication failed: no credentials provided");

			throw new BadCredentialsException(messages.getMessage(
					"AbstractUserDetailsAuthenticationProvider.badCredentials",
					"Bad credentials"));
		}

		String presentedPassword = authentication.getCredentials().toString();
		if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
			logger.debug("Authentication failed: password does not match stored value");

			throw new BadCredentialsException(messages.getMessage(
					"AbstractUserDetailsAuthenticationProvider.badCredentials",
					"Bad credentials"));
		}
	}

继承AbstractUserDetailsAuthenticationProvider抽象类

package com.larkmt.cn.admin.filter;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.util.Assert;

/**
 * @BelongsProject: larkmidtable-web
 * @BelongsPackage: com.larkmt.cn.admin.filter
 * @Author: yanhongwei
 * @CreateTime: 2023-05-11  15:01
 * @Description: TODO
 * @Version: 1.0
 */
public class MaxkeyAuthenticationProvider  extends AbstractUserDetailsAuthenticationProvider {
   
    // ~ Static fields/initializers
    // =====================================================================================

    /**
     * The plaintext password used to perform
     * PasswordEncoder#matches(CharSequence, String)}  on when the user is
     * not found to avoid SEC-2056.
     */
    private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";

    // ~ Instance fields
    // ================================================================================================

    private PasswordEncoder passwordEncoder;

    /**
     * The password used to perform
     * {@link PasswordEncoder#matches(CharSequence, String)} on when the user is
     * not found to avoid SEC-2056. This is necessary, because some
     * {@link PasswordEncoder} implementations will short circuit if the password is not
     * in a valid format.
     */
    private volatile String userNotFoundEncodedPassword;

    private UserDetailsService userDetailsService;

    private UserDetailsPasswordService userDetailsPasswordService;

    public MaxkeyAuthenticationProvider() {
   
        setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
    }

    // ~ Methods
    // ======================================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring SecurityJWT(JSON Web Token)是两个不同的概念和技术,但可以结合使用来实现单点登录Spring Security是一个强大的身份验证和访问控制框架,用于保护应用程序的安全性。它提供了一组功能丰富的工具和库,用于处理认证、授权和安全配置等方面的任务。 JWT是一种用于在网络应用中传递信息的开放标准(RFC 7519)。它由三部分组成:头部、载荷和签名。头部包含有关生成和验证JWT的元数据,载荷包含有关用户或其他实体的信息,签名用于验证JWT的完整性。 单点登录(SSO)是一种身份验证机制,允许用户使用一组凭据(例如用户名和密码)登录到一个系统后,即可无需再次输入凭据即可访问其他系统。 要实现基于Spring SecurityJWT单点登录,可以遵循以下步骤: 1. 用户成功登录到主系统(例如系统A),该系统生成并返回一个JWT给用户。 2. 用户尝试访问其他受保护的系统(例如系统B)时,在请求中包含JWT。 3. 系统B接收到请求后,使用公钥验证JWT的签名,并解析出用户信息。 4. 如果JWT验证成功且用户有权限访问系统B,则允许用户访问系统B。 需要注意的是,要实现真正的单点登录,还需要一个中心化的身份验证系统(例如OAuth2认证服务器),用于生成和验证JWT,并为不同的系统颁发相同的JWT。 此外,还可以使用Spring Security提供的一些配置来简化JWT的集成,例如自定义认证过滤器、授权管理器和认证提供者等。 总结起来,Spring SecurityJWT可以结合使用来实现单点登录Spring Security负责处理认证和授权,JWT用于传递用户信息和验证用户身份。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值