【问题标题】:Spring Boot OAuth2 - access to secured resource without authentication from specified addressSpring Boot OAuth2 - 无需从指定地址进行身份验证即可访问安全资源
【发布时间】:2018-08-05 02:40:06
【问题描述】:

我有一个问题。我想使用 Spring Boot 创建简单的微服务应用程序。我有两个微服务:带有端口 8001auth-service,用于验证用户并返回令牌,以及带有端口 8002db-service /strong>,从数据库中检索数据。在 db-service 我有一个简单的休息控制器,在这个控制器中我有一个方法,女巫通过用户名找到用户,但默认情况下这个方法是安全的,我想从 auth-service 无需身份验证。该怎么做?

这是一个例子

DbServiceController.java

@RestController
public class DbServiceController {

    /**
     * This method is secured by default, so if I want to
     * access to this resouce I need to have a token,
     * but I want to prevent authentication for auth-service,
     * because I want to retrieve token by user, which was provided by this microservice
     */
    @RequestMapping(value = "user", method = RequestMethod.GET)
    public User findByUsername(@RequestParam("username") String username) {
        return new User("john", "doe", new String[] {"ROLE_USER"});
    }

}

感谢您的所有回答。

【问题讨论】:

    标签: spring-boot oauth-2.0


    【解决方案1】:

    考虑 API 网关模式:

    http://microservices.io/patterns/apigateway.html

    https://howtodoinjava.com/spring/spring-cloud/spring-cloud-api-gateway-zuul/

    API 网关可以将不同的请求路由到不同的微服务,例如:

    • 发往:http://yourhost.com/users 的请求可以路由到一个微服务:http://microservice1/users

    • 对:http://yourhost.com/data 的请求可以路由到另一个微服务:http://another-microservice/data

    您还可以在 API 网关上实现安全性,使其成为唯一可供用户使用的服务。因此,在这种情况下,您的所有微服务都可以在没有安全性的情况下在专用网络中进行通信,但用户将在 API 网关上进行身份验证。如果您不存储敏感数据就足够了。在其他情况下,我建议观看此视频:https://www.youtube.com/watch?v=dq4RzFdm1Y0,它解释了如何正确保护微服务。

    【讨论】:

    • 嗯..这真是个好概念,但我不知道如何实现它。我想这样做:如果我键入地址localhost:8001/user/john(默认情况下是安全的),我希望浏览器会抛出异常“访问此资源需要完全身份验证”,但我想从 访问auth-service,未经授权
    猜你喜欢
    • 2018-09-11
    • 2020-11-21
    • 2020-10-25
    • 2015-01-08
    • 2016-10-03
    • 2018-01-18
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多