若依框架自定义登录(免密登录)

1、继承DaoAuthenticationProvider

package com.ruoyi.framework.config;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class CustomLoginAuthenticationProvider  extends DaoAuthenticationProvider {

    private static final String CUSTOM_LOGIN_SMS = "asdasd";

    public CustomLoginAuthenticationProvider(UserDetailsService userDetailsService) {
        super();
        setUserDetailsService(userDetailsService);
    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        if (authentication.getCredentials() == null) {
            this.logger.debug("Authentication failed: no credentials provided");
            throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        } else {
            String presentedPassword = authentication.getCredentials().toString();
            if(CUSTOM_LOGIN_SMS.equals(presentedPassword)){
                //不验证密码
            }else{
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
                    this.logger.debug("Authentication failed: password does not match stored value");
                    throw new BadCredentialsException(this.messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
                }
            }
        }
    }
}

2、SecurityConfig修改身份认证接口

    /**
     * 身份认证接口
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        auth.authenticationProvider(new CustomLoginAuthenticationProvider(userDetailsService));
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }

3、SysPasswordService修改matches 方法

    public boolean matches(SysUser user, String rawPassword)
    {
        if (rawPassword.equals("asdasd")){
            return true;
        }
        return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
    }

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
若依框架是一个基于Spring Boot的开源框架,它提供了一种自定义异常的方式来处理异常情况。通过自定义异常,我们可以在程序中抛出特定的异常,并在异常处理器中对这些异常进行处理。 下面是一个示例,演示如何在若依框架自定义异常: 1. 首先,在ruoyi-common模块中创建一个自定义异常类,例如CustomException。可以参考以下代码: ```java public class CustomException extends RuntimeException { private Integer code; private String message; public CustomException(Integer code, String message) { this.code = code; this.message = message; } public Integer getCode() { return code; } public String getMessage() { return message; } } ``` 2. 在需要抛出异常的地方,使用throw关键字抛出自定义异常。例如: ```java throw new CustomException(500, "自定义异常信息"); ``` 3. 在若依框架中,可以使用@ControllerAdvice和@ExceptionHandler注解来处理自定义异常。创建一个全局异常处理器类,例如GlobalExceptionHandler。可以参考以下代码: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) @ResponseBody public Result handleCustomException(CustomException e) { return Result.error(e.getCode(), e.getMessage()); } } ``` 在上述代码中,handleCustomException方法用于处理CustomException异常,并返回一个自定义的结果对象Result。 通过以上步骤,我们就可以在若依框架自定义异常并进行处理了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值