簡體   English   中英

使用OAuth2 SSO在spring-boot應用程序中禁用CSRF

[英]Disable CSRF in spring-boot application with OAuth2 SSO

我有一個spring-boot服務,使用OpenID Connect / OAuth2通過Okta Platform API對用戶進行身份驗證。 當用戶嘗試訪問我的服務時,他們會被重定向到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)。 它使用null參數直接調用home()方法。

我按照這篇博文來創建我的服務https://developer.okta.com/blog/2017/03/21/spring-boot-oauth

如何在仍然使用Okta的OAuth2 SSO身份驗證的同時完全禁用CSRF?

我遇到了完全相同的問題。 在深入了解HttpSecurity類如何工作之后,我修改了configure()函數,如下所示:

    @EnableWebSecurity
public static class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests().anyRequest().authenticated().and().oauth2Login().and().csrf().disable();
    }
}

添加構建器子句authorizeRequests()。anyRequest()。authenticated()。和()。oauth2Login()強制控制器使用OAuth身份驗證攔截請求。 您將在Spring HttpSecurity.java源代碼中找到完整的文檔。

為了確保我修改了所有的REST端點方法以將令牌作為參數包含在內,我可以看到configure()方法的原始版本令牌為null但是一旦我添加了額外的子句,就包含了令牌。

您只需要為/ app端點禁用CSRF令牌。 試試這個:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.mvcMatcher("/app").csrf().disable();
}
@Configuration
@EnableOAuth2Sso
public class Config extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}

這不適合我 相反,我為每個帖子請求添加了csrf標頭。 以下是獲取csrf令牌的代碼。

如果您使用的是thymeleaf,請添加以下內容以獲取.html文件中的CSRF令牌

<html xmlns:th="http://www.thymeleaf.org">
<script th:inline="javascript">
function getToken(){
        var _csrf_token = [[${_csrf.token}]] ;
        var _csrf_param_name = [[${_csrf.parameterName}]];
        console.log(_csrf_token);
        return _csrf_token;
    }
</script>
</html>

然后把它作為標題放在你的ajax請求中

function sampleFunc(url, token) {


        $.ajax({
            type : 'POST',
            contentType : "application/json",
            url : '/storeTokens',
            beforeSend : function(xhr) {
                xhr.setRequestHeader('X-CSRF-TOKEN', token);
            },
            data : jsonObject
        // dataType : 'html'
        }).done(function(data) {
            console.log("success");

        }).fail(function(jqXhr) {
            var a = jqXhr;
        });

    }

這對我有用..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM