【发布时间】:2018-04-18 22:45:10
【问题描述】:
我使用 Jhispter 开发了一个搜索 API,如果用户通过身份验证,它会返回带有用户收藏夹的搜索结果(例如图片)。现在我想返回相同的结果,但没有匿名用户的用户收藏。当我在 SecurityConfiguration 中添加到 antMatchers 的路由时,API 变得不安全。虽然我仍在发送令牌,但安全上下文得到一个空值。我想我的问题是如何在发送令牌以获得个性化结果时允许匿名用户但仍然具有安全上下文。
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers(HttpMethod.OPTIONS, "/**")
.antMatchers("/v2/api-docs")
.antMatchers("/swagger-ui/index.html")
.antMatchers("/api/b2c/register")
.antMatchers("/api/b2c/activate")
.antMatchers("/api/b2c/reset_password/init")
.antMatchers("/api/b2c/reset_password/finish")
.antMatchers("/api/contact-us")
.antMatchers("/api/search/picture")
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.httpBasic().realmName("server")
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.requestMatchers().antMatchers("/oauth/authorize")
.and()
.authorizeRequests()
.antMatchers("/oauth/authorize").authenticated();
}
【问题讨论】:
-
Welcome To StackOverflow - 请阅读我们的How to Ask 页面以改进此问题
-
你能分享你的安全配置吗?
-
当然。非常感谢。
-
您可以尝试从
web.ignoring()中删除匹配器并将其添加到具有permitAll()访问权限的OAuth2ServerConfiguration 中吗? -
我已将匹配器从 web ignoring 移至 OAuth2ServerConfiguration 并尝试使用 permitAll() 和匿名访问。仍然是同样的问题,但现在没有令牌访问被拒绝,但使用令牌可以正常工作。
标签: spring spring-security jhipster