SpringSecurity-OAuth2万文详解

SpringSecurity-OAuth2万文详解

https://github.com/spring-projects/spring-security-oauth/blob/main/spring-security-oauth2/src/test/resources/schema.sql

server-gitee:https://gitee.com/golden-Kingdragon/springsecurity-oauth2-server-demo.git
client-gitee:https://gitee.com/golden-Kingdragon/springsecurity-oauth2-client-demo.git
https://blog.csdn.net/asfasfasgjkl/article/details/126405117

    /**
     * 配置被允许访问此认证服务器的客户端详细信息
     * 1.内存管理
     * 2.数据库管理方式
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                //客户端名称
                .withClient("test-pc")
                //客户端密码
                .secret(passwordEncoder.encode("123456"))
                //资源id,商品资源
                .resourceIds("oauth2-server")
                //授权类型, 可同时支持多种授权类型
                .authorizedGrantTypes("authorization_code", "password", "implicit","client_credentials","refresh_token")
                //授权范围标识,哪部分资源可访问(all是标识,不是代表所有)
                .scopes("all")
                // false 跳转到授权页面手动点击授权,true 不用手动授权,直接响应授权码
                .autoApprove(false)
                .redirectUris("http://www.baidu.com/")//客户端回调地址
                ;
    }

AuthorizationServerConfigurerAdapter demo1

public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("srh-producer-web")
                .authorizedGrantTypes("password")
                .accessTokenValiditySeconds(accessTokenExpireSeconds)
                .refreshTokenValiditySeconds(refreshTokenExpireSeconds)
                .scopes("read", "write")
                .secret(passwordEncoder.encode("122119352"))
                .resourceIds("ms-core", "ms-user", "ms-auth")
                .autoApprove(true);
    }

WebSecurityConfigurerAdapter demo2

 /**
     * 用户类信息
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication()
                .withUser("admin")
                .password(passwordEncoder.encode("123456"))
                .authorities("admin_role")
                ;
    }

Spring Security对OAuth2提供了默认可访问端点,即URL

​ ​/oauth/authorize​ ​​:申请授权码code,涉及类​ ​AuthorizationEndpoint​ ​
​ ​/oauth/token​ ​​:获取令牌token,涉及类​ ​TokenEndpoint​ ​
​ ​/oauth/check_token​ ​​:用于资源服务器请求端点来检查令牌是否有效,涉及类​ ​CheckTokenEndpoint​ ​
​ ​/oauth/confirm_access​ ​​:用于确认授权提交,涉及类​ ​WhitelabelApprovalEndpoint​ ​
​ ​/oauth/error​ ​​:授权错误信息,涉及​ ​WhitelabelErrorEndpoint​ ​
​ ​/oauth/token_key​ ​​:提供公有密匙的端点,使用JWT令牌时会使用,涉及类​ ​TokenKeyEndpoint​ ​

Thymeleaf-extras-Spring Security

thymeleaf-extras-springsecurity4
Thymeleaf-extras-Spring Security 权限控制

https://blog.csdn.net/wangmx1993328/article/details/88946175/
更多关于 Thymeleaf-Extras-SpringSecurity 插件的内容参考官网:https://github.com/thymeleaf/thymeleaf-extras-springsecurity

Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。它的功能特性如下:

Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染
模板中的表达式支持Spring表达式语言(Spring EL)
表单支持,并兼容Spring MVC的数据绑定与验证机制
国际化支持
Spring官方也推荐使用Thymeleaf,所以本篇代码整合就使用Thymeleaf来整合。

NekoHTML 是一个 Java语言的 HTML扫描器和标签补全器(tag balancer) ,使得程序能解析HTML文档并用标准的XML接口来访问其中的信息。

Spring Loaded 是一个 JVM 代理,可以在 JVM 运行时重新加载类文件的更改。
maven 是个管理工具,如果我们不告诉它我们的代码要使用什么样的 jdk 版本编译的话,它就会用 maven-compiler-plugin 默认的 jdk 版本来进行处理,这样就容易出现版本不匹配,以至于可能导致编译不通过的问题。

maven 的默认编译使用的 jdk 版本有时候不通用,使用 maven-compiler-plugin 插件可以指定项目源码的 jdk 版本,编译后的 jdk 版本,以及编码。

maven-compiler-plugin

maven-compiler-plugin 插件是一个 Maven 插件,用来编译项目代码;自从3.0开始默认的编译器是 javax.tools.JavaCompiler,用来编译 Java 源码;如果你想强制插件使用 javac 编译器,你必须配置插件的属性 forceJavacCompilerUse;还要注意,当前默认源(source)设置为 1.6,默认目标(target)设置为 1.6。独立运行 Maven 和 JDK,可以通过 source 和 target 选项更改他们的默认值;

  1. maven-compiler-plugin插件当前最高版本是3.8.0下载地址:http://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin

  2. 插件设置的各种参数信息请查看

http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#forceJavacCompilerUse

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

image
https://blog.csdn.net/qq_40366738/article/details/129572204

@EnableAuthorizationServer// 这个注解告诉 Spring 这个应用是 OAuth2 的授权服务器

#spring:
#  security:
#    oauth2:
#      client:
#        registration:
#          gitee:
#            client-id: 470a02dc7ecb9070aee191ca87606d136bd3c78d6f05cf8e7660bb8b16c7f24b
#            client-secret: 0cbb0dfe94b28b82e3782587b7aa1351fb23972bad55f64eaf245a80ccf59bab
#            authorization-grant-type: authorization_code
#            redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
#            client-name: Gitee
#          github:
#            client-id: b4713d47174917b34c28
#            client-secret: 898389369c2e9f3d1d0ff4543ba1d9b45adfd093
#        provider:
#          gitee:
#            authorization-uri: https://gitee.com/oauth/authorize
#            token-uri: https://gitee.com/oauth/token
#            user-info-uri: https://gitee.com/api/v5/user
#            user-name-attribute: name
server:
  port: 8081
spring:
  security:
    oauth2:
      client:
        registration:
          alongcode:
            client-id: along
            client-secret: code
            authorization-grant-type: authorization_code
#            authorization-grant-type: client_credentials
            redirect-uri: 'http://127.0.0.1:8081/code'
            client-name: alongcode
#          github:
#            client-id: asfasfasgw312d
#            client-secret: afasfsafasfasfafasf
#          monkey:
#            client-id: monkey
#            client-secret: monkey123
        provider:
          alongcode:
            authorization-uri: http://192.168.3.60:8008/oauth/authorize
            token-uri: http://192.168.3.60:8008/oauth/token


# 注意:这里必须要配置provider。否则会提示以下错误。
# Provider ID must be specified for client registration 'gitee'
#  Unknown provider ID 'gitee'
OAuth2的流程与其它三种授权模式,这几种授权模式复杂程度由大至小:授权码模式 > 隐式授权模式 > 密码模式 > 客户端模式
本文要讲的是最后一种也是最简单的模式:客户端模式
其中客户端模式的流程是:客户端使用授权服器给的标识与secret访问资源服务器获取token

image

https://blog.csdn.net/weixin_43732955/article/details/119491622
https://www.cnblogs.com/hellxz/p/12041588.html

OpenFeign简介和使用详解

Feign是一个声明式的Web服务客户端(Web服务客户端就是Http客户端),让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。

cloud官网介绍Feign:https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/

OpenFeign源码:https://github.com/OpenFeign/feign

Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务。Feign的使用方式是:使用Feign的注解定义接口,调用这个接口,就可以调用服务注册中心的服务。

OpenFeign是Spring Cloud 在Feign的基础上支持了SpringMVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

RestTemplate调用rest接口传递请求参数

https://blog.csdn.net/lifei_212/article/details/84314066
https://www.cnblogs.com/jiangger/p/14894626.html

参考

https://zhuanlan.zhihu.com/p/496695229?utm_id=0
https://github.com/hellxz/spring-security-oauth2-learn
https://blog.csdn.net/aa2397199142/article/details/49496749
https://baijiahao.baidu.com/s?id=1711611224331910173&wfr=spider&for=pc
https://zhuanlan.zhihu.com/p/409184804
https://blog.csdn.net/weixin_43888891/article/details/126171740
https://www.php.cn/faq/583823.html

posted @ 2023-09-15 16:56  三里清风18  阅读(202)  评论(0)    收藏  举报