如何解決springboot2集成oauth2踩坑的問題,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)公司堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10多年網(wǎng)站建設(shè)經(jīng)驗成都創(chuàng)新互聯(lián)公司是成都老牌網(wǎng)站營銷服務(wù)商,為您提供做網(wǎng)站、成都做網(wǎng)站、網(wǎng)站設(shè)計、H5場景定制、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、小程序定制開發(fā)服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。
剛開始用springboot1.5集成oauth3沒問題,現(xiàn)在升級成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后一直停留在登陸頁面上
在網(wǎng)上找了下有以下方案: 1、配置server.servlet.session.cookie.name=UPSESSIONID 但是這個試了沒效果 2、設(shè)置code策略authCodeProvider.setStateMandatory(false); 這里改動了很多代碼
@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)到指定頁面。
看完上述內(nèi)容,你們掌握如何解決springboot2集成oauth2踩坑的問題的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞名稱:如何解決springboot2集成oauth2踩坑的問題
URL地址:http://www.chinadenli.net/article28/gpdpjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站改版、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)