1.Sso
即多个站点共用一台认证授权服务器,用户在站点登录后,可以免登录访问其他所有站点
Oauth2 协议 授权第三方进行认证
resource owner:资源所有者,这里可以理解为用户。 security (yyl-123456)
client:客户端,可以理解为一个第三方的应用程序 即微博 CSDN。(oauth admin 123456)
authorization server:(sso)
认证/授权服务器,它认证resource owner的身份,为 resource owner提供授权审批流程,并最终颁发授权令牌(Access Token)。
resource server:资源服务器: 除了认证服务器之外的其他的服务器
微服务项目里面 认证服务器只能有一台
资源服务器 多台
2.Oauth2
* authorization_code 授权码模式
* password 密码模式
* implicit 简单模式
* client_credentials 客户端模式
目的: 颁发token
获取token 校验token
认证服务器里面的
常用的路径:
/oauth/authoriz | 授权端点 |
/oauth/token | 令牌端点 获取token |
/oauth/confirm_access | 用户确认授权提交端点 |
/oauth/error | 授权服务错误信息端点 |
/oauth/check_token | 用于资源服务访问的令牌解析端点 |
/oauth/token_key | 提供公有密匙的端点,如果你使用JWT(RSA)令牌的 |
3.认证服务器:
先加jar
<!--oauth2-->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1.授权模式
访问路径
http://localhost:8081/oauth/authorize?response_type=code&client_id=admin&scop=all
如上图所示 code的值就是授权码
2.简单模式:
http://localhost:8081/oauth/authorize?response_type=token&client_id=admin&scope=all
https://www.baidu.com/#access_token=4a30aba6-16ef-497b-b139-dd932fd86427&token_type=bearer&expires_in=43199
3.客户端模式
必须有gran_type参数其他参数无所谓
4.密码模式:
不输入账号密码会显示让你登录
可以配置
就可以不用登录了,如下图
1.校验token
localhost:8081/oauth/check_token?token=2113e2e3-8ba1-4db1-aa9c-b6f6607aac12
jwt格式
校验
正常路径登录生成token
.successHandler((request, response, authentication) -> {
String username = request.getParameter("username");
String password = request.getParameter("password");
HttpRequest post = HttpUtil.createPost("localhost:8081/oauth/token");
post.form("grant_type","password");
post.form("client_id","admin");
post.form("client_secret","123456");
post.form("grant_type","password");
post.form("username",username);
post.form("password",password);
HttpResponse execute = post.execute();
String body = execute.body();
System.out.println(body);
JSONObject entries = JSONUtil.parseObj(body);
Object accessToken = entries.get("access_token");
printJsonData(response,new Result(200,"成功",accessToken));
})
5.资源服务器
1.加jar
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security.oauth.boot/spring-security-oauth2-autoconfigure -->
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
2.编写配置文件
package com.example.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("lhj");
return jwtAccessTokenConverter;
}
}
3.写正常的逻辑
![](https://i-blog.csdnimg.cn/blog_migrate/c0c737b14adb55757a868b82abdc0aa1.png)
4.测试
token必须放在头里
key必须为Authorization
token 必须要bearer 类型