[英]Spring security provider for web login and api security
我正在构建一个使用spring mvc 4作为框架的Web服务。 我的网络服务具有api,签名为/ api / v1 / apiname
我的服务由外部服务调用,并且必须进行身份验证。 我更喜欢为服务使用预先授权的令牌,我可以阅读并进行授权。
如果是我的网站,该网站托管在同一服务上,则使用sso进行登录,该网站基本上使用cookie来验证用户身份。
我开发了部分代码,该代码支持使用标头令牌和SSO登录。 但是我需要确保所有api都应在未经身份验证的情况下返回401,并且网页访问应重定向到外部sso服务身份验证目的。
http.anonymous().disable()
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/resources/protected**").denyAll()
.antMatchers("/**").fullyAuthenticated()
.and()
.csrf().disable()
.logout().logoutUrl("/logout").logoutSuccessUrl("/")
.and()
.addFilterBefore(new TokenAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class)
.addFilterBefore(new CookieAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class);
这里的CookieAuthenticationFilter将执行基于cookie的身份验证...我的问题是,如果所有api请求都应返回401作为状态,而所有其他请求应重定向到外部服务,以防未经授权的访问。 我该如何实现。
它应该与该问题的答案非常相似。 您将需要配置多个HttpSecurity对象,并且可以为每个对象配置不同的行为。 Spring文档在这里有一个不错的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.