英文:
Spring boot security JwtAuthentication and OAuth together
问题
我尝试着让jwtAuthentication在除了"/directory/auth"之外的所有路由上都起作用,我需要这个路由使用oauth工作并返回jwt令牌,但我无法让它们同时正常工作。
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable()
.cors().configurationSource(corsConfigurationSource());
/// 路由"/authenticate"是我的jwtAuthentication,"/directory/auth"是我的oauth
httpSecurity.authorizeRequests()
.antMatchers("/authenticate",
"/directory/auth").permitAll()
.anyRequest().authenticated().and()
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
// 尝试在一个路由上设置oauth
.authorizeRequests()
.antMatchers("/directory/auth").authenticated().and()
.oauth2Login()
.userInfoEndpoint()
.oidcUserService(oidcUserService);
// 其他过滤器
httpSecurity.addFilterBefore(decryptInterceptor, UsernamePasswordAuthenticationFilter.class);
httpSecurity.addFilterAfter(encryptInterceptor, UsernamePasswordAuthenticationFilter.class);
httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
希望对您有所帮助。
英文:
i was trying that jwtAuthentication works on all routes except one "/directory/auth" this one i need that works with oauth and return a jwt token but i cant get that both work together
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable()
.cors().configurationSource(corsConfigurationSource());
/// the route "/authenticate" is my jwtAuthentication and "/directory/auth" is my oauth
httpSecurity.authorizeRequests()
.antMatchers("/authenticate",
"/directory/auth").permitAll().
anyRequest().authenticated().and()
.exceptionHandling()
.authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
// trying to set oauth on one route
.authorizeRequests()
.antMatchers("/directory/auth").authenticated().and()
.oauth2Login()
.userInfoEndpoint()
.oidcUserService(oidcUserService);
//others filters
httpSecurity.addFilterBefore(decryptInterceptor, UsernamePasswordAuthenticationFilter.class);
httpSecurity.addFilterAfter(encryptInterceptor, UsernamePasswordAuthenticationFilter.class);
httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
}
Thanks
专注分享java语言的经验与见解,让所有开发者获益!
评论