Spring Cloud整合SpringSecurity OAuth2(全网最强)

前言

本文是梳理整合SpringCloud和SpringSecurity OAuth2的搭建流程!好久没撸SpringSecurity OAuth2这系列代码了,都快忘了,特写此文章梳理脉络!开干!!!

Maven版本

微服务版本

			<spring-boot.version>2.3.2.RELEASE</spring-boot.version>
			<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
			<spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>

			<!-- spring cloud alibaba 依赖 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- spring cloud 依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

SpringSecurity OAuth2版本

		<!--安全模块-->
        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
        </dependency>

通过微服务版本限定后spring-security-oauth2-autoconfigure的最终版本自动适配为2.1.2

授权码模式

刚开始我这里就不一次性把一大堆配置放上来,需要什么就写什么,不然到时候都搞不清那个配置是干嘛,有什么用的!这也是我写这个文章的缘由!

授权服核心配置-AuthorizationServerConfig

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

}

SpringSecurity核心配置

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

}

启动服务器-访问测试
访问http://localhost:3000/oauth/authorize?response_type=code&client_id=tao&redirect_uri=http://baidu.com&scope=all

在这里插入图片描述

任意输入账号密码试试
在这里插入图片描述
这是因为我们啥也没配置!

配置密码加密
WebSecurityConfig中

  	@Bean
    public PasswordEncoder passwordEncoder() {//密码加密
        return new BCryptPasswordEncoder();
    }

配置登录用户账号密码
WebSecurityConfig中

 	@Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("123456")).roles("USER","ADMIN").authorities(AuthorityUtils.commaSeparatedStringToAuthorityList("p1,p2"));
        //这里配置全局用户信息
    }

授权服配置端点信息
AuthorizationServerConfig

@Autowired
    PasswordEncoder passwordEncoder;

@Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        //基于内存便于测试
        clients.inMemory()// 使用in-memory存储
                .withClient("tao")// client_id
                //.secret("secret")//未加密
                .secret(passwordEncoder.encode("secret"))//加密
                //.resourceIds("res1")//资源列表
                .authorizedGrantTypes("authorization_code", "password", "client_credentials", "implicit", "refresh_token")// 该client允许的授权类型authorization_code,password,refresh_token,implicit,client_credentials
                .scopes("all", "ROLE_ADMIN", "ROLE_USER")// 允许的授权范围
                //.autoApprove(false)//false跳转到授权页面
                //加上验证回调地址
                .redirectUris("http://baidu.com");
    }

重启服务测试
http://localhost:3000/oauth/authorizeresponse_type=code&client_id=tao&redirect_uri=http://baidu.com&scope=all
在这里插入图片描述
登录成功得到授权码
在这里插入图片描述
授权码获取token
在这里插入图片描述
这里授权码就基本搞定了!接下来我们试试密码模式
在这里插入图片描述
Unsupported grant type: password,默认不支持密码模式,需要而外配置下!

密码模式

配置认证管理器-AuthenticationManager
WebSecurityConfig中

	@Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

授权服配置密码模式
AuthorizationServerConfig

	@Autowired
    private AuthenticationManager authenticationManager;


	@Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {//配置令牌的访问端点和令牌服务
        endpoints
                .authenticationManager(authenticationManager)//认证管理器
               ;

    }

重启访问测试
在这里插入图片描述
成功!

简化模式

这个就简单了,token直接是显示在地址栏上的http://localhost:3000/oauth/authorize?client_id=tao&response_type=token&scope=all&redirect_uri=http://baidu.com

在这里插入图片描述
客户端模式这里就不演示了,实际上用的并不多!那么到这里,授权基本上的就搞定了,至于其他配置下文会深入,这里我们既然得到了Token那么我们就可以测试一下认证!

认证测试

创建测试资源

  • 23
    点赞
  • 171
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值