Skip to content

Dev 新增 sso 模块 #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<module>spring-boot-demo-elasticsearch-rest-high-level-client</module>
<module>spring-boot-demo-https</module>
<module>spring-boot-demo-flyway</module>
<module>spring-boot-demo-sso-server</module>
<module>spring-boot-demo-sso-client1</module>
<module>spring-boot-demo-sso-client2</module>
</modules>
<packaging>pom</packaging>

Expand Down Expand Up @@ -134,6 +137,13 @@
<artifactId>UserAgentUtils</artifactId>
<version>${user.agent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
1 change: 1 addition & 0 deletions spring-boot-demo-https/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</dependencies>

<build>
<finalName>spring-boot-demo-https</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.xkcoding.springbootdemohttps;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootDemoHttpsApplicationTests {

@Test
void contextLoads() {
}

}
6 changes: 6 additions & 0 deletions spring-boot-demo-sso-client1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


## sso 单点登录系统 客户端


全流程详见 服务端`spring-boot-demo-sso-server`
43 changes: 43 additions & 0 deletions spring-boot-demo-sso-client1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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>com.xkcoding</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<groupId>spring-boot-demo-sso-client1</groupId>
<artifactId>spring-boot-demo-sso-client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo-sso-client1</name>
<description>Demo project for Spring Boot</description>

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

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>2.0.1.RELEASE</version>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.xcoding.sso;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
* @author Administrator
*/
@SpringBootApplication
public class SpringBootDemoSsoClient1Application {

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


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.xcoding.sso.config;

import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
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.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
* @author chen.chao
* @version 1.0
* @date 2020/4/16 9:41
* @description
*/

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableOAuth2Sso
public class ClientWebSecurityConfigurer extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
// 需要先于需授权地址
http.antMatcher("/**").authorizeRequests().antMatchers("/free").permitAll()
.anyRequest().authenticated();
/* http.antMatcher("/**").authorizeRequests()
.anyRequest().authenticated().antMatchers("/free").permitAll();*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xcoding.sso.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author chen.chao
* @version 1.0
* @date 2020/4/14 17:25
* @description
*/
@RestController
public class IndexController {


@GetMapping("/free")
public String normal( ) {
return "不需要授权路径!";
}

@GetMapping("/user")
@PreAuthorize("hasAuthority('ROLE_USER')")
public String medium() {
return "用户权限路径";
}

@GetMapping("/admin")
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
public String admin() {
return "管理员权限路径";
}
}
23 changes: 23 additions & 0 deletions spring-boot-demo-sso-client1/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


# sso-server地址
auth-server: http://localhost:9900/uac

server:
port: 9901
security:
oauth2:
client:
client-id: client1
client-secret: client1
#请求认证的地址
user-authorization-uri: ${auth-server}/oauth/authorize
#请求令牌的地址
access-token-uri: ${auth-server}/oauth/token
resource:
jwt:
#解析jwt令牌所需要密钥的地址
key-uri: ${auth-server}/oauth/token_key

debug: true

6 changes: 6 additions & 0 deletions spring-boot-demo-sso-client2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


## sso 单点登录系统 客户端


全流程详见 服务端`spring-boot-demo-sso-server`
34 changes: 34 additions & 0 deletions spring-boot-demo-sso-client2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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">
<parent>
<artifactId>spring-boot-demo</artifactId>
<groupId>com.xkcoding</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-demo-client2</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>2.0.1.RELEASE</version>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.xcoding.sso;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
* @author Administrator
*/
@SpringBootApplication
public class SpringBootDemoSsoClient2Application {

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


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xcoding.sso.config;

import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
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.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
* @author chen.chao
* @version 1.0
* @date 2020/4/16 9:41
* @description
*/

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableOAuth2Sso
public class ClientWebSecurityConfigurer extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**").authorizeRequests().antMatchers("/free").permitAll()
.anyRequest().authenticated();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xcoding.sso.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author chen.chao
* @version 1.0
* @date 2020/4/14 17:25
* @description
*/
@RestController
public class IndexController {


@GetMapping("/free")
public String normal( ) {
return "不需要授权路径!";
}

@GetMapping("/user")
@PreAuthorize("hasAuthority('ROLE_USER')")
public String medium() {
return "用户权限路径";
}

@GetMapping("/admin")
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
public String admin() {
return "管理员权限路径";
}
}
23 changes: 23 additions & 0 deletions spring-boot-demo-sso-client2/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


# sso-server地址
auth-server: http://localhost:9900/uac

server:
port: 9902
security:
oauth2:
client:
client-id: client2
client-secret: client2
#请求认证的地址
user-authorization-uri: ${auth-server}/oauth/authorize
#请求令牌的地址
access-token-uri: ${auth-server}/oauth/token
resource:
jwt:
#解析jwt令牌所需要密钥的地址
key-uri: ${auth-server}/oauth/token_key

debug: true

Loading