SpringCloud Alibaba微服务实战三十七 - Oauth2自定义登录接口

简介: SpringCloud Alibaba微服务实战三十七 - Oauth2自定义登录接口

大家好,我是飘渺。(今天又来给大家送书了~

有不少人私下问我,为什么SpringCloud alibaba实战系列不更新了,主要是因为大部分核心功能都已经讲完了,剩下的基本是属于业务功能开发了,需要根据实际业务扩展。

今天更新文章的原因是粉丝提了个问题:如何实现Oauth2认证服务器自定义登录接口以及返回自定义格式? 这里我给大家分享一个简单且实用的方法,既可以灵活定制登录参数也可以自行组装返回结果。


实现方案


我们知道,认证服务器生成token的入口是TokenEndpoint#postAccessToken(Principal principal, @RequestParam Map<String, String> parameters),那我们就可以直接在认证服务器自定义一个登录接口,然后组装好TokenEndpoint#postAccessToken()需要的参数,直接调用它生成token后再封装成我们需要的格式即可。

接下来我们直接进入实战:

1. 定义登录参数

/**
 * 自定义登录参数
 * @author JAVA日知录
 * @date 2022/5/14 09:23
 */
@Data
public class LoginRequest {
    private String userName;
    private String password;
    private String grantType;
    private String mobile;
    private String smsCode;
}

为了兼容密码模式和自定义的短信验证码模式,我们将所有的参数都放入一个实体,大家可以根据自己的项目需要自行封装。

2. 创建一个登录类型的枚举

public enum LoginTypeEnum {
    /**
     * 密码模式
     */
    PASSWORD("password"),
    /**
     * 短信验证码模式
     */
    SMSCODE("sms_code");
    private final String grantType;
    LoginTypeEnum(String grantType) {
        this.grantType = grantType;
    }
    public String getGrantType() {
        return grantType;
    }
    public static LoginTypeEnum fromGrantType(String grantType){
        return Arrays.stream(LoginTypeEnum.values())
                .filter(item -> item.getGrantType().equals(grantType))
                .findFirst()
                .orElseThrow(()-> new BizException("不支持此登录类型"));
    }
}

3. 创建自定义登录接口(关键)

@RestController
@RequestMapping("/token")
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuthController {
    private final TokenStore tokenStore;
    private final TokenEndpoint tokenEndpoint;
    private final RedisTemplate<String,String> redisTemplate;
    /**
     * 自定义登录接口
     * @return
     */
    @PostMapping("login")
    public ResultData<OAuth2AccessToken> login(@RequestBody LoginRequest loginRequest) throws HttpRequestMethodNotSupportedException {
        Assert.isTrue(StringUtils.isNotEmpty(loginRequest.getGrantType()), "请在参数中指定登录类型grantType");
        LoginTypeEnum typeEnum = LoginTypeEnum.fromGrantType(loginRequest.getGrantType());
        //注入clientId 和 password
        // 可以通过Header传入client 和 secret
        User clientUser = new User("jianzh5", "jianzh5", new ArrayList<>());
        Authentication token = new UsernamePasswordAuthenticationToken(clientUser, null, new ArrayList<>());
        //构建密码登录
        Map<String, String> map = new HashMap<>();
        switch (typeEnum){
            case PASSWORD : {
                map.put("username", loginRequest.getUserName());
                map.put("password", loginRequest.getPassword());
                map.put("grant_type", "password");
                break;
            }
            case SMSCODE:{
                map.put("smsCode", loginRequest.getSmsCode());
                map.put("mobile", loginRequest.getMobile());
                map.put("grant_type", "sms_code");
                break;
            }
            default: throw new BizException("不支持的登录类型");
        }
        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(token,map).getBody();
        return ResultData.success(oAuth2AccessToken);
    }
  ...
}

这里我们将TokenEndpoint注入,然后伪装一个客户端的认证流程,调用TokenEndpoint.postAccessToken()获取接口。

这里我们写死了client信息,实际上也可以通过Header请求头传入或者通过配置文件注入。

4. 在安全配置类中放行登录接口

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  ...
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 加入验证码登陆
        http.apply(smsCodeSecurityConfig);
        http
                .authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
                .and()
                .authorizeRequests().antMatchers("/token/**","/sms/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf()
                .disable();
    }
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(
                "/error",
                "/static/**",
                "/v2/api-docs/**",
                "/swagger-resources/**",
                "/webjars/**",
                "/favicon.ico"
            );
    }
}

这个安全配置类中有两个放行策略,一个通过permitAll()实现,一个通过web.ignoring()实现,他们两个的区别是:

web ignoring()比较适合配置前端相关的静态资源,它是完全绕过spring security的所有filter的;permitAll(),会给没有登录的用户适配一个AnonymousAuthenticationToken,设置到SecurityContextHolder,方便后面的filter可以统一处理authentication。

{
"userName":"zhangjian",
"password":"111111",
"grantType":"password",
"mobile":"18888887777",
"smsCode":"666666"
}


测试


1. 自定义密码登录

2. 自定义短信验证码登录

3. 不支持的登录类型


小结


本文提供的方案是将登录接口与认证服务器放在一起,如果在项目中由于某些原因不方便将其放在认证服务中,也可以让认证服务器提供一个Feign接口,然后让后端服务调用此接口进行登录即可。

好了,今天就到这儿吧,我是飘渺,我们下期见~~

目录
相关文章
|
3天前
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
22 5
|
14天前
|
负载均衡 Java 开发者
Spring Cloud微服务架构中的配置管理与服务发现
Spring Cloud微服务架构中的配置管理与服务发现
|
12天前
|
消息中间件 负载均衡 Java
最容易学会的springboot gralde spring cloud 多模块微服务项目
最容易学会的springboot gralde spring cloud 多模块微服务项目
|
6天前
|
消息中间件 供应链 Java
实现基于Spring Cloud的事件驱动微服务
实现基于Spring Cloud的事件驱动微服务
|
10天前
|
微服务
SpringCloud01微服务课程导学,微服务功能用户,支付,购物车,积分,优惠卷,短信功能越来越多
SpringCloud01微服务课程导学,微服务功能用户,支付,购物车,积分,优惠卷,短信功能越来越多
|
13天前
|
负载均衡 Java 开发者
Spring Cloud微服务架构中的配置管理与服务发现
Spring Cloud微服务架构中的配置管理与服务发现
|
14天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【7月更文挑战第1天】Spring Cloud是Java微服务治理明星框架,整合Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心),提供完整服务治理解决方案。通过Eureka实现服务注册与发现,Ribbon进行客户端负载均衡,Hystrix确保服务容错,Config Server集中管理配置,Zuul作为API网关简化系统复杂性。理解和使用Spring Cloud是现代Java开发者的关键技能。
23 0
|
4天前
|
运维 Kubernetes 监控
深入解析微服务架构的演进与实践
本文旨在探究微服务架构从诞生到成熟的发展历程,分析其背后的技术推动力和业务需求,并结合具体案例,揭示实施微服务过程中的挑战与解决策略。通过对微服务架构与传统单体架构的对比,阐明微服务如何优化现代应用开发流程,提高系统的可扩展性、可维护性和敏捷性。
14 0
|
2天前
|
监控 负载均衡 安全
探索微服务架构中的API网关模式
【7月更文挑战第13天】在微服务架构的海洋中,API网关犹如一座灯塔,指引着服务间的通信和客户端请求。本文将深入剖析API网关的核心作用、设计考量以及实现策略,为构建高效、可靠的分布式系统提供实践指南。
18 10
|
1天前
|
弹性计算 运维 Kubernetes
自动化运维的新篇章:容器编排与微服务架构
【7月更文挑战第14天】在数字化转型的浪潮中,企业对运维效率和系统可靠性的需求日益增长。本文深入探讨了自动化运维的最新趋势——容器编排和微服务架构,并阐述了如何通过这些技术提升运维效率、降低系统复杂性以及提高服务的可用性和可扩展性。文章不仅介绍了相关技术和工具的选择,还提供了实际案例分析,旨在为读者提供一套完整的解决方案框架,以适应快速变化的市场需求。