oauth2.0 02 授权码模式

授权码 模式

将服务启动成功 然后地址栏访问:
http://localhost:8888/oauth/authorize?client_id=client&scope=app&response_type=code

(这个地址中的client_id=client 和 scope=app 都是AuthorizationServerConfig类中配置的 )

 这里的登录名的admin 和 123456 是我们代码WebSecurityConfiguration中配置的

 

在 spring1 的基础上新增2个类

package com.example.demo.auth.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

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


    /**
     * 配置登陆用户信息,密码需要加密处理
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password(passwordEncoder().encode("123456"))
                .roles("ADMIN")
                .and()
                .withUser("user").password(passwordEncoder().encode("123456")).roles("USER");
    }

}

AuthorizationServerConfig 类中的 .authorizedGrantTypes("authorization_code") 就表示使用的授权类型是 授权码方式

package com.example.demo.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

import javax.annotation.Resource;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {


    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients()
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("permitAll()");
    }

    /**
     * 配置客户端信息(注意 这里不是登陆用户信息,而是可以访问系统的客户端)
     *
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.
                //使用内存设置
                        inMemory()
                //客户端
                .withClient("client")
                //客户端密码
                .secret(passwordEncoder.encode("secret"))
                //授权类型
                // http://localhost:8888/oauth/authorize?client_id=client&scope=app&response_type=code
                .authorizedGrantTypes("authorization_code")
                //授权范围
                .scopes("app")
//                .autoApprove(false)
//                .accessTokenValiditySeconds(60)//秒
//                .refreshTokenValiditySeconds(60)
                //注册回调地址
                .redirectUris("http://www.baidu.com");

    }


}

启动服务访问: http://localhost:8080/oauth/authorize?client_id=client&scope=app&response_type=code

 会在地址栏 有一个 授权码 这个授权码 很重要

然后通过这个 授权码 就可以拿到我们服务给的token 信息,接下来 我们 用postman 来测试一下获取 token:

首先 在AuthorizationServerConfig类中(也就是用 了@EnableAuthorizationServer注解的类)添加一下代码,重写了configure方法

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //开启表单认证,主要是让/oauth/token支持client_id以及client_secret做登录验证
        security.allowFormAuthenticationForClients()
                //开启/oauth/token_key验证端口无权限访问
                .tokenKeyAccess("permitAll()")
                //开启/oauth/check_token验证端口认证无限性访问
                .checkTokenAccess("permitAll()");
    }

请求的时候 表单中的code就是 页面授权成功的时候在地址栏中出现的,即:,其他表单参数都是我们在系统代码中配置的,也就是内存中

生成的授权码如下:

{

    "access_token": "ca84fec2-643a-4645-91f0-42220d2b2bfc",

    "token_type": "bearer",

    "expires_in": 43199,

    "scope": "app"

}

 到此就结束了请求。

补充 也可以 在表单中的两个参数 挪到 Authorization中,然后取消勾选下图中的参数,也可以请求通过:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值