美文网首页易水组件入门示例
基于易水公共组件的资源服务器配置

基于易水公共组件的资源服务器配置

作者: 易水风萧 | 来源:发表于2019-10-30 19:39 被阅读0次

在使用本组件之前,需要先搭建好认证服务器,其中认证服务器可以根据业务需要进行单机或集群部署。

整个系统的结果如图所示

image

在整个系统中 ,认证服务器的作用是产生token和验证token的有效性,资源服务器是根据认证服务器的响应结果决定如何处理请求资源。

1 引入依赖

完整的pom依赖文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yishuifengxiao.sso-client</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--需要引入的组件-->
        <dependency>
            <groupId>com.yishuifengxiao.common</groupId>
            <artifactId>common-spring-boot-starter</artifactId>
            <version>4.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.yishuifengxiao.common</groupId>
            <artifactId>oauth2-resource-starter</artifactId>
            <version>1.1.0</version>
        </dependency>
    <!--需要引入的组件-->
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2 在项目中加入文件

@Configuration
@EnableWebSecurity
public class SecurityConfig extends AbstractSecurityConfig {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

3 在配置中加入配置属性

主要加入的配置属性如下:

# 只想认证服务器的token校验地址
yishuifengxiao.security.resourceserver.userInfoUrl=http://192.168.0.172:8000/oauth/check_token
# 表示在出现异常时直接输出json响应
yishuifengxiao.security.handler.exception.returnType=json

4 资源管理

资源管理的用法请参见 易水公共组件设置

5 访问资源

在项目里加入以下代码

@SpringBootApplication
@RestController
public class DemoApplication {

    @GetMapping("/me")
    public Authentication user(Authentication authentication) {

        return authentication;

    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

    }

}

完成上述配置以后,先从认证服务器中获取到access_token,然后再利用access_token访问资源服务器中的资源了。

例如当我们想要访问 /me时,即可通过 http://localhost:8080/me?access_token=认证服务器里获得token获取到资源信息了。

token的用法即可参见 access_token 使用


资源链接

易水公共组件官方文档地址:http://doc.yishuifengxiao.com/

易水公共组件源码地址:https://gitee.com/zhiyubujian/common-starter

易水风萧个人博客 http://www.yishuifengxiao.com

相关博客

  1. springboot整合单点登录sso

  2. 搭建基于易水公共组件的资源服务器

相关文章

  • 基于易水公共组件的资源服务器配置

    在使用本组件之前,需要先搭建好认证服务器,其中认证服务器可以根据业务需要进行单机或集群部署。 整个系统的结果如图所...

  • 基于易水公共组件的单点登录

    如今,随着互联网技术的发展,网络用户规模越来越大,假如公司的每一个应用都建立一个用户系统,不仅极大的增加了开发的工...

  • 基于易水公共组件的权限管理系统

    快速启动 在使用spring security时,可以参考以下步骤 在项目中加入 spring security依...

  • 基于易水公共组件的全局异常捕获

    1 引入相关依赖 引入完成易水公共组件后,项目即可自动开启全局异常捕获功能了。 2 配置错误信息提示 在一般情况下...

  • 搭建oauth2.0 授权服务器

    pom.xml 配置授权服务器 配置资源服务器 编写资源api 启动类 readme 基于授权码模式+Spring...

  • SSHD 服务(CentOS7)

    安全验证方法 基于口令:账号+密码 基于密钥:本地生产密钥对,公钥传到服务器,进行公共密钥比较 服务配置文件 启动...

  • Spring Cloud Config服务器

    服务器为外部配置(名称值对或等效的YAML内容)提供了基于资源的HTTP。服务器可以使用@EnableConfig...

  • Spring Cloud Config服务器

    服务器为外部配置(名称值对或等效的YAML内容)提供了基于资源的HTTP。服务器可以使用@EnableConfig...

  • Spring Cloud Config—服务器

    服务器为外部配置(名称值对或等效的YAML内容)提供了基于资源的HTTP。服务器可以使用@EnableConfig...

  • java版spring cloud+spring boot+re

    服务器为外部配置(名称值对或等效的YAML内容)提供了基于资源的HTTP。服务器可以使用@EnableConfig...

网友评论

    本文标题:基于易水公共组件的资源服务器配置

    本文链接:https://www.haomeiwen.com/subject/ypsavctx.html