溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何解決springboot2集成oauth2踩坑的問題

發(fā)布時(shí)間:2021-09-28 10:06:21 來(lái)源:億速云 閱讀:260 作者:柒染 欄目:大數(shù)據(jù)

如何解決springboot2集成oauth2踩坑的問題,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問題。


剛開始用springboot1.5集成oauth3沒問題,現(xiàn)在升級(jí)成springboot2.1踩了不少坑,下面列舉下:

問題一
Possible CSRF detected - state parameter was required but no state could be found

客戶端代碼

@EnableOAuth3Sso
@Configuration
public class UiSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/", "/login**")
                .permitAll()
                .anyRequest()
                .authenticated();
    }

}

在獲取到code后一直停留在登陸頁(yè)面上 如何解決springboot2集成oauth2踩坑的問題 在網(wǎng)上找了下有以下方案: 1、配置server.servlet.session.cookie.name=UPSESSIONID 但是這個(gè)試了沒效果 2、設(shè)置code策略authCodeProvider.setStateMandatory(false); 這里改動(dòng)了很多代碼

@Configuration
@EnableOAuth3Client
@EnableGlobalMethodSecurity(prePostEnabled=true)//開啟@PreAuthorize注解
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private OAuth3ClientContext oauth3ClientContext;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        // @formatter:off
        http.authorizeRequests()
                .anyRequest().authenticated().and()
                .formLogin().loginPage("/login").permitAll().and()
                .exceptionHandling().and()
                .logout().logoutSuccessUrl("/login").permitAll()
                .and().headers().frameOptions().sameOrigin()
                .and().csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
                .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);//這里需要配置在basic前
    }
    @Bean
    public FilterRegistrationBean oauth3ClientFilterRegistration(OAuth3ClientContextFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(filter);
        registration.setOrder(-100);
        return registration;
    }

    @Bean
    @ConfigurationProperties("security.oauth3")
    public ClientResources trina() {
        return new ClientResources();
    }

    private Filter ssoFilter() {
        CompositeFilter filter = new CompositeFilter();
        List<filter> filters = new ArrayList<filter>();
        filters.add(ssoFilter(trina(), "/login"));
        filter.setFilters(filters);
        return filter;
    }
    private Filter ssoFilter(ClientResources client, String path) {
        OAuth3ClientAuthenticationProcessingFilter oAuth3ClientAuthenticationFilter = new OAuth3ClientAuthenticationProcessingFilter(
                path);
        OAuth3RestTemplate oAuth3RestTemplate = new OAuth3RestTemplate(client.getClient(), this.oauth3ClientContext);
        oAuth3ClientAuthenticationFilter.setRestTemplate(oAuth3RestTemplate);
        AuthorizationCodeAccessTokenProvider authCodeProvider = new AuthorizationCodeAccessTokenProvider();
        authCodeProvider.setStateMandatory(false);
        AccessTokenProviderChain provider = new AccessTokenProviderChain(
                Arrays.asList(authCodeProvider));
        oAuth3RestTemplate.setAccessTokenProvider(provider);
        UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(),
                client.getClient().getClientId());
        tokenServices.setRestTemplate(oAuth3RestTemplate);
        oAuth3ClientAuthenticationFilter.setTokenServices(tokenServices);
        return oAuth3ClientAuthenticationFilter;
    }

}

class ClientResources {

    @NestedConfigurationProperty
    private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();

    @NestedConfigurationProperty
    private ResourceServerProperties resource = new ResourceServerProperties();

    public AuthorizationCodeResourceDetails getClient() {
        return client;
    }

    public ResourceServerProperties getResource() {
        return resource;
    }
}

修改后訪問呢連接,登陸后成功跳轉(zhuǎn)到指定頁(yè)面。

看完上述內(nèi)容,你們掌握如何解決springboot2集成oauth2踩坑的問題的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI