本页将介绍Spring Security
OAuth2
@EnableOAuth2Sso
注解的例子。
@EnableOAuth2Sso
注解可以启用OAuth2
单点登录(SingleSignOn,SSO
)。默认情况下,所有的路径都是需要安全的。
我们可以在Spring Security
的Java
配置中使用WebSecurityConfigurerAdapter
来定制它。我们可以使用application.properties
或application.yml
或以命令行方式配置Spring Security OAuth2
。
这里我们将使用GitHub
创建Spring Boot OAuth2
应用程序。
演示工具版本
- Java 11
- Spring 5.1.7.RELEASE
- Spring Boot 2.1.5.RELEASE
- Maven 3.5.2
Maven 依赖
找到OAuth2
的Maven
依赖。
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
在Spring Boot
应用程序中,上述依赖关系在类路径上的可用性为我们提供了自动配置OAuth2
的优势。
使用 @EnableOAuth2Sso
要在我们的应用程序中使用@EnableOAuth2Sso
,请在Spring Security
配置中用@Configuration
对其进行注释。
@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration {
}
现在所有的URL都是需要安全验证的。我们可以使用WebSecurityConfigurerAdapter
来自定义这一行为。假设我们想使用一些不进行安全验证的URL
,如主页和错误页面等,如下进行配置。
SecurityConfiguration.java
package com.concretepage;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableOAuth2Sso
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/error**").permitAll()
.anyRequest().authenticated()
.and().logout().logoutUrl("/logout")
.logoutSuccessUrl("/");
}
}
OAuth2 配置
在Spring Boot
应用程序中,我们可以使用application.properties
或application.yml
或以命令行方式配置安全OAuth2
客户端、资源和sso
属性。
在我们的例子中,我们使用的是GitHub OAuth
。
application.yml
security:
oauth2:
client:
clientId: <your_github_clientId>
clientSecret: <your_github_clientSecret>
accessTokenUri: https://github.com/login/oauth/access_token
userAuthorizationUri: https://github.com/login/oauth/authorize
clientAuthenticationScheme: form
resource:
userInfoUri: https://api.github.com/user
sso:
login-path: /login
你需要在上述YML
文件中输入你的GitHub
的clientId
和 clientSecret
。
clientId: 这是OAuth
客户端的ID
,OAuth
提供商通过它来识别客户端。
clientSecret: 与资源关联的客户端密钥。
要获得GitHub
的OAuth2
客户端ID
和客户端密钥,请通过该链接。
登出
要注销Spring Security
应用程序,请在Spring Security
Java
配置文件中配置注销URL
,默认为/logout
,然后创建一个表单并以POST
方式提交给注销URL
。用Thymeleaf
查找示例表单。
<form th:action="@{/logout}" method="POST">
<input type="submit" value="Logout"/>
</form>
完整示例
这里我们将提供我们的演示程序的完整代码。文章中已经给出了SecurityConfiguration.java
和application.yml
这两个文件。查找其余的代码。
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath />
</parent>
<properties>
<context.path>spring-app</context.path>
<java.version>11</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
AppController.java
package com.concretepage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class AppController {
@GetMapping("hello")
public ModelAndView welcome() {
ModelAndView mav = new ModelAndView();
mav.setViewName("welcome");
return mav;
}
@GetMapping("error")
public ModelAndView error() {
ModelAndView mav = new ModelAndView();
return mav;
}
}
index.html
<!doctype html>
<html>
<head>
<title>Spring Security</title>
</head>
<body>
<h3>Login with <a href="/hello">GitHub</a></h3>
</body>
</html>
welcome.html
<!doctype html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
Welcome <b th:inline="text" > [[${#httpServletRequest.remoteUser}]] </b> <br/><br/>
<form th:action="@{/logout}" method="POST">
<input type="submit" value="Logout"/>
</form>
</body>
</html>
error.html
<!doctype html>
<html>
<head>
<title>Spring Security</title>
</head>
<body>
<h3>Error</h3>
<p thif="${param.error}">
An error occurred.
</p>
</body>
</html>
Main.java
package com.concretepage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
输出
下载该项目,并在application.yml
文件中输入你的GitHub
clientId
和clientSecret
。
然后使用命令提示符从项目的根文件夹中运行以下命令。
mvn spring-boot:run
访问网址
http://localhost:8080/
点击GitHub
链接进行登录。你将被重定向到GitHub
登录页面。登录成功后,你将被重定向到你的应用程序并看到欢迎页面。
参考文献
【1】OAuth2 Boot
【2】OAuth 2 Developers Guide
源码下载
提取码:mao4