Sso配置

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.写正常的逻辑

4.测试

token必须放在头里

key必须为Authorization

token 必须要bearer 类型

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值