oauth password模式_SpringBoot OAuth2.0 认证授权(密码模式)

398f692a33f984b0c769b1beab2edbb6.png

SpringBoot 整合 SpringSecurity,token 落地,前后端分离接口安全。

SpringBoot 环境搭建和入门:Spring Boot 2.x 快速入门

导入 mysql 脚本

包含用户表,oauth2.0 数据脚本

https://gitee.com/shizidada/moose-resource/blob/master/moose-security.sql

全部 : https://gitee.com/shizidada/moose/blob/master/src/main/resources/moose.sql

导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
​
<dependency>
    <groupId>org.springframework.security.oauth.boot</groupId>
    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

创建 WebSecurity 配置文件

@Slf4j
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
public class MooseWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
​
  @Bean
  @Override
  public UserDetailsService userDetailsServiceBean() throws Exception {
    
    return new UserDetailsServiceImpl();
  }
​
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    
    auth.userDetailsService(userDetailsServiceBean());
  }
​
  /**
   * 用于支持 password 模式 密码模式需要 AuthenticationManager 支持
   * password 模式一点要加上这个
   * @throws Exception
   */
  @Bean
  @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
    
    return super.authenticationManagerBean();
  }
}

创建 AuthorizationServer 配置

/**
 * 资源认证配置
 * <p>
 * Description:
 * </p>
 *
 * @author taohua
 * @version v1.0.0
 * @see com.moose.operator.web.security.configure
 * <p>
 * [/oauth/authorize] [/oauth/token] [/oauth/check_token]
 * [/oauth/confirm_access] [/oauth/token_key] [/oauth/error]
 */
@Configuration
@Enabl
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值