【发布时间】:2017-03-26 07:17:15
【问题描述】:
我使用 Spring Boot 开发了一个 RESTful Web 服务。输入 URL 后,将返回 JSON 资源。服务器端并不完全符合JSON API,但它可以工作。
现在我想通过简单的 HTTP 基本身份验证来保护 RESTful 服务。简单来说,如果客户端发送 HTTP GET 以便访问
http://mycompany.com/restful/xyz
它将收到 HTTP 未经身份验证的错误,除非请求配置了正确的 Authorization basic XXXXXXXX。 xxxxxx 是 user:pwd
我想用 Spring Security 来做这件事。经过一番谷歌搜索后,我可能需要创建一些类,例如:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
....
}
@Override
protected void configure(HttpSecurity http) throws Exception {
....
}
}
有两点我需要了解:
我发现的大多数 Spring Security 示例都在某种程度上与使用 Spring MVC 的 Web 应用程序相关,但我确信它可以在如上所示的场景中使用 - 一个独立的 Web 服务(在 Tomcat 中好吧,但是不是网络应用);
-
任何人都可以在上面的两种方法中显示一些代码sn-p,其效果是只允许某些用户/密码将过滤器传递给资源,
http://mycompany.com/restful/xyz否则返回 HTTP 认证错误码。
谁能帮忙?
【问题讨论】:
标签: web-services spring-mvc spring-security spring-boot restful-authentication