在我的sprintboot oauth2应用程序中,所有端点都重定向到默认的redirecturi。
例子:
http://localhost:8080/欢迎>欢迎
http://localhost:8080/安全欢迎>重定向到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应用程序注册:
2条答案
按热度按时间wz3gfoph1#
拆下
redirectUri
从application.properties
,使“手动”重定向如下:myss37ts2#
有两个问题:
除了github注册之外,代码没有问题。
重定向URI必须是
http://localhost:8080/login/oauth2/code/github
由于我在代理服务器下,上面的uri不起作用,我最终使用了错误的值。因此,为了使这种情况正确,我们需要相应地传递下面的vm参数。
这使得这个方案可行。