Spring Security在身份验证和未经身份验证的用户中获得REST服务中的用户信息 [英] Spring Security get user info in rest service, for authenticated and not authenticated users
问题描述
我有一个spring rest服务,我想将其用于经过身份验证的用户,而不是经过身份验证的用户.如果用户已通过身份验证,我想从SecurityContextHolder.getContext().getAuthentication()
获取用户信息.
I have a spring rest service, I want to use it for authenticated and not authenticated users. And I want to get user information from SecurityContextHolder.getContext().getAuthentication()
if user is authenticated.
- 如果我使用
.antMatchers("/app/rest/question/useroperation/list/**").permitAll()
在如下所示的ouath2配置中,那么我可以获取有关的用户信息 经过身份验证的用户,但对于未经身份验证的用户,显示401错误. - 如果我
.antMatchers("/app/rest/question/useroperation/list/**").permitAll()
并通过以下方式忽略WebSecurity中的urlweb.ignoring()..antMatchers("/app/rest/question/useroperation/list/**")
在SecurityConfiguration
中,如下所示,那么所有用户都可以调用 服务,但我无法从SecurityContext获取用户信息.
- If I use
.antMatchers("/app/rest/question/useroperation/list/**").permitAll()
in ouath2 configuration like below, then I can get user info for authenticated user, but 401 error for not authenticated users. - If I
.antMatchers("/app/rest/question/useroperation/list/**").permitAll()
and ignore the url in WebSecurity byweb.ignoring()..antMatchers("/app/rest/question/useroperation/list/**")
inSecurityConfiguration
like below, then all users can call the service, but I cant get user information from SecurityContext.
如何配置我的spring安全性,以便为经过身份验证和未经身份验证的用户调用url,并在用户登录后从SecurityContext获取用户信息.
How can configure my spring security to call a url for authenticated and not authenticated users and get user info from SecurityContext if user logged in.
@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Inject
private Http401UnauthorizedEntryPoint authenticationEntryPoint;
@Inject
private AjaxLogoutSuccessHandler ajaxLogoutSuccessHandler;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.logout()
.logoutUrl("/app/logout")
.logoutSuccessHandler(ajaxLogoutSuccessHandler)
.and()
.csrf()
.requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
.disable()
.headers()
.frameOptions().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/views/**").permitAll()
.antMatchers("/app/rest/authenticate").permitAll()
.antMatchers("/app/rest/register").permitAll()
.antMatchers("/app/rest/question/useroperation/list/**").permitAll()
.antMatchers("/app/rest/question/useroperation/comment/**").authenticated()
.antMatchers("/app/rest/question/useroperation/answer/**").authenticated()
.antMatchers("/app/rest/question/definition/**").hasAnyAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/app/rest/logs/**").hasAnyAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/app/**").authenticated()
.antMatchers("/websocket/tracker").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/websocket/**").permitAll()
.antMatchers("/metrics/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/health/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/trace/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/dump/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/shutdown/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/beans/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/info/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/autoconfig/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/env/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/trace/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/api-docs/**").hasAuthority(AuthoritiesConstants.ADMIN)
.antMatchers("/protected/**").authenticated();
}
}
SecurityConfiguration
SecurityConfiguration
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Inject
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder() {
return new StandardPasswordEncoder();
}
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/bower_components/**")
.antMatchers("/fonts/**")
.antMatchers("/images/**")
.antMatchers("/scripts/**")
.antMatchers("/styles/**")
.antMatchers("/views/**")
.antMatchers("/i18n/**")
.antMatchers("/swagger-ui/**")
.antMatchers("/app/rest/register")
.antMatchers("/app/rest/activate")
.antMatchers("/app/rest/question/useroperation/list/**")
.antMatchers("/console/**");
}
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true)
private static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
return new OAuth2MethodSecurityExpressionHandler();
}
}
}
推荐答案
permitAll()
仍然需要Authentication
对象出现在SecurityContext
中.
permitAll()
still requires Authentication
object to present in SecurityContext
.
对于非oauth用户,可以通过启用匿名访问来实现:
For not oauth users this can be achieved with anonymous access enabled:
@Override
public void configure(HttpSecurity http) throws Exception {
http
//some configuration
.and()
.anonymous() //allow anonymous access
.and()
.authorizeRequests()
.antMatchers("/views/**").permitAll()
//other security settings
匿名访问将向在AnonymousAuthenticationToken
中填充AnonymousAuthenticationToken
作为身份验证信息的过滤器链中添加其他过滤器:AnonymousAuthenticationFilter
,以防SecurityContext
Anonymous access will add additional filter: AnonymousAuthenticationFilter
to the filter chain that populate AnonymousAuthenticationToken
as Authentication information in case no Authentication
object in SecurityContext
这篇关于Spring Security在身份验证和未经身份验证的用户中获得REST服务中的用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!