[英]SpringBoot OAUTH2 always redirecting to default redirecturi
在我的 SprintBoot OAuth2 應用程序中,所有端點都重定向到默認的 redirectUri。
例子:
http://localhost:8080/歡迎 > 歡迎
http://localhost:8080/secure-welcome > 重定向到 github > 成功 > 歡迎(預計Secure Welcome
)
http://localhost:8080/secure-welcome2 > 已經認證/授權 > 歡迎(預計Secure Welcome 2
)
@SpringBootApplication
@RestController
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@GetMapping(value = "/welcome")
public String welcome() {
return "Welcome";
}
@GetMapping(value = "/secure-welcome")
public String secureWelcome() {
return "Secure Welcome";
}
@GetMapping(value = "/secure-welcome2")
public String secureWelcome2() {
return "Secure Welcome 2";
}
}
@Configuration
public class Config extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth2/authorization/**", "/welcome").permitAll()
.antMatchers("/secure**").authenticated()
.and().oauth2Login();
}
}
屬性文件:
spring.security.oauth2.client.registration.github.client-id=<client id>
spring.security.oauth2.client.registration.github.client-secret=<client secret>
spring.security.oauth2.client.registration.github.redirect-uri=http://localhost:8080/welcome
spring.security.oauth2.client.registration.github.authorization-grant-type=authorization_code
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 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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.testing</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</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-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
GitHub 應用注冊:
從application.properties
中刪除redirectUri
,像這樣進行“手動”重定向:
httpServletResponse.setHeader("Location", "http://localhost:8080/secure-welcome");
httpServletResponse.setStatus(302);
有幾個問題:
RedirectURI 必須是http://localhost:8080/login/oauth2/code/github
因此,為了使這種情況正確,我們需要相應地傳遞以下 VM 參數。
-Dhttp.proxyHost={your proxy}
-Dhttp.proxyPort={your port number}
-Dhttps.proxyHost=your proxy}
-Dhttps.proxyPort={your port number}
-Dhttp.nonProxyHosts=localhost|127.0.0.1
這使得場景工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.