使用Spring Security保护REST

时间:2014-06-09 02:58:18

标签: java spring-security oauth-2.0 spring-boot

我在运行这个项目https://github.com/royclarkson/spring-rest-service-oauth时遇到了一些问题。我克隆它并运行mvn spring-boot:run,一旦在端口8080上启动Tomcat,我就从浏览器访问它:

http://localhost:8080/greeting

但它说,An Authentication object was not found in the SecurityContextunauthorized

1 个答案:

答案 0 :(得分:0)

解决方案是访问资源:

curl -X POST -vu clientapp:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp"

回复将如下:

{"access_token":"ff16372e-38a7-4e29-88c2-1fb92897f558","token_type":"bearer","expires_in":43199,"scope":"read write"}

然后放上令牌:

curl http://localhost:8080/greeting -H "Authorization: Bearer ff16372e-38a7-4e29-88c2-1fb92897f558"