认证服务器里的常用路径
路径 | 说明 |
/oauth/authoriz | 授权端点 |
/oauth/token | 令牌端点 获取token |
/oauth/confirm_access | 用户确认授权提交端点 |
/oauth/error | 授权服务错误信息端点 |
/oauth/check_token | 用于资源服务访问的令牌解析端点 |
/oauth/token_key | 提供公有密匙的端点,如果你使用JWT(RSA)令牌的 |
编写oauth配置类
在编写SecurityConfiguration配置类
授权码模式
使用http://localhost:8050/oauth/authorize?response_type=code&client_id=admin&scop=all
获取授权码
使用第三方软件请求得出结果
=========================================================================
简单模式
直接访问路径
返回路径会显示具体参数
客户端模式
填写相对应数据即可
安全模式
安全配置
添加jwt依赖
配置bean
使用bean
获取到 jwt 类型的 token
编写
搭载资源服务器
新建service模块
添加依赖
再其中在创建一个model
修改配置文件
但是可以直接访问不安全所以增加配置类
package com.aaa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(jsr250Enabled = true,prePostEnabled = true,securedEnabled = true)
public class MyResConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
//.access("@RbacConfig.hasPermission(request,authentication)")
.and()
.csrf().disable();
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(){
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
jwtAccessTokenConverter.setSigningKey("dev");
return jwtAccessTokenConverter;
}
}
测试资源是否生效
携带token 访问资源服务器,Authorization 放在headers里面的,Token的值 要求必须是bearer 类型的 token前面加上这个类型