如何在不使用身份验证的情况下保护restful webservice

时间:2015-09-23 13:58:53

标签: web-services rest spring-security spring-boot spring-rest

有人可以告诉我使用spring rest来保证在spring boot项目中编写的restful web服务的所有方法(没有用户凭据检查,因为这个服务是由位于不同服务器上的远程应用程序调用的)

问题陈述:

我有一个rest类和一个方法,应由另一个远程应用程序访问。远程应用程序不会发送除正文内容和内容类型之外的任何内容。在这种情况下,如何保护此休息服务,以便只能通过该特定远程应用程序访问服务。

@RequestMapping("/rest")
@RestController 
public class WorkflowController {

    @RequestMapping(value = "ticket/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public Long startWorkflow(@RequestBody TicketInfo ticketInfo) {
        ...//DO SOMETHING

        Long id = 1L;

        return id; // return some long value
    }

}

请说明实现这一目标的方法是什么。 提前致谢

2 个答案:

答案 0 :(得分:1)

好的,所以我不知道我是否完全理解你的问题,但是因为不同的情况而生病了。 假设您的客户端应用程序位于 静态IP你可以创建一个过滤器和一个ip地址的白名单,这非常简单,可能不够好。

如果不是这种情况,您可以使用参数GET或POST再次创建过滤器,您必须在第一次调用中发送身份验证字符串以获取身份验证。你还必须实现身份验证管理器。

        if(hsr.getParameter("ex_code") != null){ 
            String exCode= hsr.getParameter("ex_code");


            String userToken = new String( Base64.getDecoder().decode(hsr.getParameter("ex_code")));

            PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(serviceThatReturnsAUserDetailsFacade.loadUserByUsername(userToken),
                    exCode);

            token.setDetails(authenticationDetailsSource.buildDetails((HttpServletRequest) request));

            try {


                authentication = authenticationManager.authenticate(token);
             ....

答案 1 :(得分:0)

如果您不想实施任何安全保障。只是想验证主机&端口(只有一个应用程序可以在特定主机和端口上运行)并假设您使用Spring,那么您只需从传入的HttpServletRequest中获取以下内容: -

a) RemoteAddr -> IP address of machine from which request originated.
b) RemoteHost -> Host name of machine from which request originated.
c) RemotePort -> Port of machine from which request originated.

有一个接口方法可以验证这个&如果有效则允许它通过,如果无效则将相应的错误消息返回给客户端。

除此之外,还有一个其他选项也被称为"匿名授权"详情here