springboot security oauth2实用详例

2020-10-21  本文已影响0人  baituo111

本案例中ResourceServerConfigurerAdapter、WebSecurityConfigurerAdapter和AuthorizationServerConfigurerAdapter一起整合放在同一服务内,可以有多个分离的资源服务ResourceServerConfigurerAdapter可以打包部署在别的服务器上,本案例只弄两个分离的资源服务。本案例实现了无状态登陆。登陆成功后加入自定义数据返回。在token里放入自定义数据。无权限访问时自定义返回数据。无效token时自定义数据返回。同一账户在不同前端登陆时,只有最新登陆的才有效。本案例为密码模式,使用spngboot的版本为2.1.2.RELEASE。

项目结构如下


3333.jpg

父工程的pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>outdd.ccsw</groupId>
<artifactId>suout</artifactId>
<version>1.0.00</version>
<packaging>pom</packaging>

<description>Demo project for Spring Boot</description>  
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent> 
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>   

<dependencies>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>
   <dependency>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
       <version>1.2</version>
   </dependency>
   <dependency>
       <groupId>commons-io</groupId>
       <artifactId>commons-io</artifactId>
       <version>2.6</version>
   </dependency>  
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <!-- JWT 方式令牌 -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>           
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>  

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

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>  

<modules>
<module>oauthscu1</module>
<module>oauthscu2</module>
<module>oauthscu3</module>
</modules>

</project>

oauthscu1的pom.xml

   <?xml version="1.0"?>
   <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
<groupId>outdd.ccsw</groupId>
<artifactId>suout</artifactId>
<version>1.0.00</version>
  </parent>
  <groupId>outdd.ccsw</groupId>
 <artifactId>oauthscu1</artifactId>
<version>1.0.00</version>
 <name>oauthscu1</name>
 <url>http://maven.apache.org</url>
  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>  
</dependencies>
 </project>

oauthscu2工程的pom.xml

 <?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
  <groupId>outdd.ccsw</groupId>
<artifactId>suout</artifactId>
<version>1.0.00</version>
  </parent>
  <groupId>outdd.ccsw</groupId>
  <artifactId>oauthscu2</artifactId>
  <version>1.0.00</version>
  <name>oauthscu2</name>
  <url>http://maven.apache.org</url>
  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 <dependencies>
 </dependencies>
</project>

oauthscu3工程的pom.xml和oauthscu2的分别不大

 <?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <modelVersion>4.0.0</modelVersion>
 <parent>
<groupId>outdd.ccsw</groupId>
<artifactId>suout</artifactId>
<version>1.0.00</version>
  </parent>
 <groupId>outdd.ccsw</groupId>
  <artifactId>oauthscu3</artifactId>
 <version>1.0.00</version>
 <name>oauthscu3</name>
 <url>http://maven.apache.org</url>
 <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
 </dependencies>
</project>
上一篇下一篇

猜你喜欢

热点阅读