我是一名有用的助手,可以为您翻译以下内容:
这是我的控制器代码:
然而,添加上述配置后,我的服务停止了使用Okta进行用户认证(不再将未经身份验证的请求重定向到Okta),它直接调用home()方法,并传递null参数。
我按照这篇博客文章创建了我的服务 https://developer.okta.com/blog/2017/03/21/spring-boot-oauth 如何在仍然使用OAuth2 SSO认证与Okta的情况下完全禁用CSRF?
我有一个使用OpenID Connect/OAuth2通过Okta Platform API对用户进行身份验证的Spring Boot服务。当用户尝试访问我的服务时,他们将被重定向到Okta登录页面进行认证,然后Okta将它们重定向回我的服务。
这是我pom.xml文件中相关的部分
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</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>
这是我的控制器代码:
@RestController
@EnableOAuth2Sso
@SpringBootApplication
public class Application {
@RequestMapping(path = "/", method = RequestMethod.GET)
public String home(Authentication auth) {
return "Home: " + auth.getName();
}
@RequestMapping(path = "/app", method = RequestMethod.POST)
public String app(Authentication auth) {
return "App: " + auth.getName();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
对于第一个GET控制器方法,这很完美,但对于第二个POST方法,我的服务要求我提供CSRF令牌。我想完全禁用CSRF检查,所以我将其添加到我的应用程序中。
@Configuration
@EnableOAuth2Sso
public class Config extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
然而,添加上述配置后,我的服务停止了使用Okta进行用户认证(不再将未经身份验证的请求重定向到Okta),它直接调用home()方法,并传递null参数。
我按照这篇博客文章创建了我的服务 https://developer.okta.com/blog/2017/03/21/spring-boot-oauth 如何在仍然使用OAuth2 SSO认证与Okta的情况下完全禁用CSRF?
javax.security.Principal
注入到你的home()
方法中,而不是使用Authentication
吗? - Matt Raible