若依开源框架登录扩展Springboot+security,密码、验证码多种登录

若依登录自定义扩展Springboot+security支持密码、验证码多种登录方式 若依开源框架登录使用的配置大部分都是security自定义的,目前希望在此框架基础上支持自定义的登录,如手机号+密码登录认证、手机号+短信验证码认证。 1、自定义登录实现思路 主要是实现继承DaoAuthenticationProvider类,重写additionalAuthenticationChecks方法,将通过密码标识来判断是不是需要验证密码和免密验证。 2、继承DaoAuthenticationProvider package com.tuitui.framework.security. 阅读详情

自定义登录扩展类

  • 继承DaoAuthenticationProvider类型,重写additionalAuthenticationChecks方法
  • CustomLoginAuthenticationProvider.java
package com.qcn.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;

/**
 * 自定义登录扩展
 * @version 1.0
 * @author xiyan
 * @date 2020/08/11 18:24
 */
public class CustomLoginAuthenticationProvider extends DaoAuthenticationProvider {

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

    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)){
                //短信登录,不验证密码(还可以继续扩展,只要传进来的password标识即可)
            }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"));
                }
            }
        }
    }
}


修改spring security配置

在这里插入图片描述

package com.qcn.framework.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.web.filter.CorsFilter;
import com.qcn.framework.security.filter.JwtAuthenticationTokenFilter;
import com.qcn.framework.security.handle.AuthenticationEntryPointImpl;
import com.qcn.framework.security.handle.LogoutSuccessHandlerImpl;

/**
 * spring security配置
 * 
 * @author ruoyi
 */
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    /**
     * 自定义用户认证逻辑
     */
    @Autowired
    private UserDetailsService userDetailsService;
    
    /**
     * 认证失败处理类
     */
    @Autowired
    private AuthenticationEntryPointImpl unauthorizedHandler;

    /**
     * 退出处理类
     */
    @Autowired
    private LogoutSuccessHandlerImpl logoutSuccessHandler;

    /**
     * token认证过滤器
     */
    @Autowired
    private JwtAuthenticationTokenFilter authenticationTokenFilter;

    /**
     * 跨域过滤器
     */
    @Autowired
    private CorsFilter corsFilter;
    
    /**
     * 解决 无法直接注入 AuthenticationManager
     *
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception
    {
        return super.authenticationManagerBean();
    }

    /**
     * anyRequest          |   匹配所有请求路径
     * access              |   SpringEl表达式结果为true时可以访问
     * anonymous           |   匿名可以访问
     * denyAll             |   用户不能访问
     * fullyAuthenticated  |   用户完全认证可以访问(非remember-me下自动登录)
     * hasAnyAuthority     |   如果有参数,参数表示权限,则其中任何一个权限可以访问
     * hasAnyRole          |   如果有参数,参数表示角色,则其中任何一个角色可以访问
     * hasAuthority        |   如果有参数,参数表示权限,则其权限可以访问
     * hasIpAddress        |   如果有参数,参数表示IP地址,如果用户IP和参数匹配,则可以访问
     * hasRole             |   如果有参数,参数表示角色,则其角色可以访问
     * permitAll           |   用户可以任意访问
     * rememberMe          |   允许通过remember-me登录的用户访问
     * authenticated       |   用户登录后可访问
     */
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception
    {
        httpSecurity
                // CRSF禁用,因为不使用session
                .csrf().disable()
                // 认证失败处理类
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                // 基于token,所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                // 过滤请求
                .authorizeRequests()
                // 对于登录login 验证码captchaImage 允许匿名访问
                .antMatchers("/login", "/captchaImage","/api/anonymous/**").anonymous()
                .antMatchers(
                        HttpMethod.GET,
                        "/*.html",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js"
                ).permitAll()
                .antMatchers("/profile/**").anonymous()
                .antMatchers("/common/download**").anonymous()
                .antMatchers("/common/download/resource**").anonymous()
                .antMatchers("/swagger-ui.html").anonymous()
                .antMatchers("/swagger-resources/**").anonymous()
                .antMatchers("/webjars/**").anonymous()
                .antMatchers("/*/api-docs").anonymous()
                .antMatchers("/druid/**").anonymous()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().disable();
        httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
        // 添加JWT filter
        httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        // 添加CORS filter
        httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
        httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
    }

    
    /**
     * 强散列哈希加密实现
     */
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder()
    {
        return new BCryptPasswordEncoder();
    }

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

controller

	@ApiOperation("手机号+密码登录")
    @Log(title = "手机号+密码登录", businessType = BusinessType.OTHER)
    @PostMapping(value="/loginByMobile")
    public AjaxResult loginByMobile(@Validated @RequestBody LoginByPwdBody body)
    {
        CardUser entity=cardUserService.loginByMobileAndPwd(body.getMobile(),body.getPassword());
        String token=cardLoginService.login(body.getMobile(),body.getPassword());
        AjaxResult result = AjaxResult.success("登录成功!",entity);
        result.put(Constants.TOKEN, token);
        return result;
    }

    @ApiOperation("手机号+短信验证码登录")
    @Log(title = "手机号+短信验证码登录", businessType = BusinessType.OTHER)
    @PostMapping(value="/loginByCode")
    public AjaxResult loginByCode(@Validated @RequestBody LoginByCodeBody body)
    {
        CardUser entity=cardUserService.loginByMobileAndCode(body);
        String token=cardLoginService.login(body.getMobile(),"CUSTOM_LOGIN_SMS");
        AjaxResult result = AjaxResult.success("登录成功!",entity);
        result.put(Constants.TOKEN, token);
        smsService.deleteSmsRedis(body.getSource());
        return result;
    }
java若依框架开发api接口(添加新模块) springboot若依框架,通过添加模块统一管理自己接口! 阅读详情

相关推荐

若依登录自定义扩展Springboot+security支持密码、手机验证码登录方式

若依开源框架登录使用的配置大部分都是security自定义的,目前希望在此框架基础上支持自定义的登录,如手机号+密码登录认证、手机号+短信验证码认证。 1、自定义登录实现思路 主要是实现继承DaoAuthenticationProvider类,重写additionalAuthenticationChecks方法,将通过密码标识来判断是不是需要验证密码和免密验证。 2、继承DaoAuthenticationProvider` import org.springframework.security.core.A

nsq147的博客 3778

若依框架免密登录(仅做参考)

用于若依框架使用微信登录免密登录

SMJ0809的博客 5466

若依框架SpringBoot登录流程全解析:从验证码到动态路由的完整实现

本文深入解析了若依框架SpringBoot后端的登录流程,从验证码校验、Spring Security认证到动态路由生成的完整实现。详细拆解了用户认证、权限数据加载与前端资源同步的核心机制,并探讨了性能优化与安全实践,为开发者构建健壮的企业级权限系统提供实战指导。

weixin_29228781的博客 214

【若依(ruoyi)】重置密码SQL脚本

目录前言重置密码SQL脚本 前言 若依(ruoyi) 4.2 重置密码SQL脚本 将admin账户的密码重置为admin123,脚本如下: SELECT , u.* update sys_user u SET u.password=MD5(CONCAT(login_name, 'admin123', salt)) WHERE u.login_name='admin' ...

sayyy的专栏 6592

spring-security多个登录页面配置

spring-security多个登录页面配置,分开了前台登录页面和后台登录页面登录,还有注销返回的不同的界面。

阿里云java短信验证码源码-ruoyi:若依二开

阿里云java短信验证码源码 平台简介 一直想做一款后台管理系统,看了很多优秀的开源项目但是发现没有合适的。于是利用空闲休息时间开始自己写了一套后台系统。如此有了若依。她可以用于所有的Web应用程序,如网站管理后台,网站会员中心,CMS,CRM,OA。所有前端后台代码封装过后十分精简易上手,出错概率低。同时支持移动客户端访问。系统会陆续更新一些实用功能。 性别男,若依是给还没有出生女儿取的名字(寓意:你若不离不弃,我必生死相依) 若依参考后台模板。有需要可自行到群内下载。 内置功能 用户管理:用户是系统操作者,该功能主要完成系统用户配置。 部门管理:配置系统组织机构(公司、部门、小组),树结构展现支持数据权限。 岗位管理:配置系统用户所属担任职务。 菜单管理:配置系统菜单,操作权限,按钮权限标识等。 角色管理:角色菜单权限分配、设置角色按机构进行数据范围权限划分。 字典管理:对系统中经常使用的一些较为固定的数据进行维护。 参数管理:对系统动态配置常用参数。 通知公告:系统通知公告信息发布维护。 操作日志:系统正常操作日志记录和查询;系统异常信息日志记录和查询。 登录日志:系统登录日志记

Spring Security OAuth2 微服务认证中心自定义授权模式扩展以及常见登录认证场景下的应用实战

Spring Security OAuth2 默认实现的四种授权模式在实际的应用场景中往往满足不了预期,如以下需求:相信你会遇到但不仅限上面的场景,网上也有很多对 Spring Security OAuth2 授权模式扩展的相关文章,但多少有不全面和实现复杂的通病,一度会让你觉得 Spring Security OAuth2 很难, Spring 在实现核心功能基础上同时还提供了很多的扩展点,Spring Security OAuth2 亦是如此,相信这篇文章会帮助消除它很难的误解。......

有来技术 4363

化学气相沉积CVD工艺中MKS下游排气高速节流阀的国产化替代方案及产品

对标MKS、VAT和CDK等国外公司的下游排气高速节流阀系列产品,本文介绍了相应的国产化替代方案和产品。基于CVD工艺,技术方案将下游流量调节阀变为了下游压力调节阀,并采用分体结构,将调压阀与PID控制器分离,调压阀具备大口径和高速功能,电子气控驱动调压阀快速动作,PID控制器接收真空计信号和控制气控驱动阀,可实现阀门全开时间小于0.1秒。

Shanghai_Eyoung的博客 5259

img2threejs 头发管线全解:从参考图像测量到过程化发丝建模的质量门控实现

本篇技术指南深入剖析 img2threejs 项目中负责头发重建的独立子系统([docs/HAIR_PIPELINE.md](https://link.gitcode.com/i/239a40a1e606360875f5014b533893a6))。该管线以"代码而非纹理"的方式,把参考图像中的头发还原为可动画、质量受控的 Three.js 模型部件。读完本文,你将掌握:为什么头发在几何问题之外更

gitblog_00671的博客 1085

AD9176 双通道高速DAC调试记录

AD9176是一款高性能双通道16位数模转换器,支持高达12.6GSPS采样率,专为多频段无线应用设计。该器件集成8通道15.4Gbps JESD204B接口,支持复数/实数数据输入,每通道最大输入速率达3.08GSPS。关键特性包括:48位高精度NCO、灵活的插值滤波器(最高12倍)、优异的动态性能(SFDR达-83dBc@1.84GHz)以及多芯片同步功能。硬件调试需重点关注时钟配置(建议250M基准时钟)、JESD参数匹配及交流耦合设计。测试指标要求SFDR>55dBc@312MHz,实测值可达65d

universaldata的博客 1492

LTspice进阶技巧-031.一键加密仿真模型:LTspice Encrypt Tool实战指南

本文详细介绍了LTspice Encrypt Tool的使用方法,帮助工程师一键加密LTspice仿真模型,保护知识产权。通过图形化界面简化操作流程,支持批量加密和兼容性处理,适用于电路设计中的模型保护需求。

weixin_28304023的博客 202

SpringSecurity自定义多Provider时提示No AuthenticationProvider found for问题的解决方案与原理(一)

SpringSecurity 5.6.X 自定义多Provider时No AuthenticationProvider found for问题的排查与修复

半斤米粉闯天下的博客 1万+

【TM1650】STM8S TM1650芯片控制程序

以下为.h文件: #ifndef __TM1650_I2C_H #define __TM1650_I2C_H #include "stm8s.h" #include "stm8s_gpio.h" #include "tim1.h" #include "usart2.h" #include <iostm8s103f3.h> #include <intrinsics....

cxw312864660的博客 1万+

【亲测免费】 推荐开源项目:Spring-Security — 助力安全的Spring Boot应用

推荐开源项目:Spring-Security — 助力安全的Spring Boot应用 1、项目介绍 在Java开发领域,Spring Boot以其简便的启动方式和强大的集成能力深受开发者喜爱。然而,在构建高可用系统时,安全问题始终是不可忽视的一环。为此,我们向您推荐一个由Spring社区精心打造的开源项目——Spring-Security,它是一款为Spring Boot应用提供安全保障的强大框...

gitblog_00077的博客 450

MATLAB并行开不了的解决办法

在服务器上新安装了MATLAB 2013b,在开并行计算的时候出现了下列问题 经过在网上搜多这个问题,我感觉这个问题应该是安装的时候的验证问题或者激活不成功之类的。注意其中有一句validate the profile ‘local’ in the Cluster Profile Manager.在matlab官网上找到了设置Cluster Manager的教程,如下 parallel>

sinat_36713319的博客 5987

【Lua面试】2021年Lua面试题分享(4.25更新)

lua深拷贝和浅拷贝的区别?如何实现深拷贝? lua中ipairs和pairs的区别? 链接: link.

qq_21407523的博客 2万+

Echo开源社区系统:基于SpringBoot的完整论坛解决方案深度解析

你是否正在寻找一个功能完整的开源社区系统?想要快速搭建一个现代化的在线论坛?Echo开源社区系统正是你需要的终极解决方案!这个基于SpringBoot的完整论坛系统集成了当前最流行的Java Web技术栈,提供了从用户管理到内容交互的全方位功能,是新手和普通用户快速构建社区平台的理想选择。 ## 🎯 为什么选择Echo社区系统? Echo是一套前后端不分离的开源社区系统,基于目前主流Java

gitblog_00170的博客 418

YOLO11改进|卷积篇|引入多样化分支块DBB

YOLO11中添加多样化分支块DBB

A1983Z的博客 1449

Java八股文面试之框架篇(Spring、SpringMVC、Mybatis、Spring Security)

Java八股文面试之框架篇(Spring、SpringMVC、Mybatis)

y516369的博客 1304

由异常的测试眼图引发的深入思考

信号测试中出现“双眼皮”的眼图测试效果,可能的导致因素总结如下:PCB通道阻抗设计不匹配,负载端出现严重反射;发射端采用了不当的信号预处理;接收器均衡处理不当;通道串扰严重... 基于以上的假设,本文就逐一分析下这些因素到底是如何对正常的信号眼图造成影响的。

一只豌豆象的博客 3064

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

构建企业级单点登录认证中心:Spring Boot OAuth2 Server完整实战指南

在微服务架构盛行的今天,统一的身份认证与授权管理已成为企业数字化转型的关键环节。Spring Boot OAuth2 Server是一个基于Spring Boot 3+和Spring Security 6+构建的完整单点登录认证中心解决方案,为企业提供安全、高效、可扩展的身份认证服务。本文将深入解析这一项目的架构设计、核心功能以及最佳实践,帮助开发者快速搭建企业级认证授权平台。 ## 项目概述与

gitblog_00280的博客 210

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

UJCMS企业级Java开源CMS完整教程:从零搭建到高效部署

UJCMS是一款基于Java开发的企业级开源内容管理系统,采用SpringBoot、MyBatis、Spring Security、Vue3等现代技术栈构建,支持传统模板渲染与前后端分离双模式开发。本教程将带你从零开始,全面掌握UJCMS的安装部署、功能配置和实战应用技巧,助你快速搭建专业级内容管理平台。 ## 🎯 为什么选择UJCMS构建企业级网站? 在众多CMS系统中,UJCMS凭借其独

gitblog_00698的博客 1175

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

重磅来袭,权限框架的里程碑,Spring Security电子版教程已问世

form登录的流程Spring Security 中的登录HttpSecurity中的form表单登录Spring Security聚合登录实战多种登录方式并存的实现图解认证过滤器UsernamePasswordAuthenticationFilter图解认证管理器AuthenticationManagerAuthenticationManager的初始化细节。

jjc4261的博客 73

SSM+Vue大学生社团管理系统开发与验证码安全实践

在Web应用开发中,验证码技术是保障系统安全的重要防线,通过图形和短信验证码可以有效防止暴力破解和机器人攻击。其核心原理包括生成随机验证码、设置有效期和使用次数限制,结合Redis实现高效验证。SSM框架作为经典JavaEE开发组合,通过Spring的IOC容器管理Bean,SpringMVC处理Web请求,MyBatis操作数据库,适合构建中小型管理系统。Vue.js前端框架配合ElementUI组件库,能快速开发响应式管理界面。本系统针对高校社团管理场景,通过验证码模块实现安全防护,集成SSM后端与Vu

weixin_34292402的博客 389

如何用Echo实现用户认证与权限管理:Spring Security实战指南 [特殊字符]

在当今的Web应用开发中,用户认证与权限管理是保障系统安全的核心环节。Echo开源社区系统基于Spring Security框架,提供了一套完整、高效的**用户认证与权限管理**解决方案。无论你是Spring Boot新手还是有一定经验的开发者,本指南都将带你深入了解Echo如何实现安全可靠的用户认证与权限控制机制。 ## 📋 核心概念:Echo的用户权限体系 Echo系统采用经典的三级权限

gitblog_00489的博客 820
上一篇: window下mysql5.7以上免安装版教程:mysql5.7.31下载安装配置、mysql环境变量配置以及mysql卸载
下一篇: 身份证号验证
夕颜以北
博客等级 码龄9年 30粉丝 46原创
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值