代码之家  ›  专栏  ›  技术社区  ›  Abhilash Muthuraj

使用硬编码令牌访问spring boot Rest端点?

  •  0
  • Abhilash Muthuraj  · 技术社区  · 7 年前

    我试图公开一个REST-GET端点,它允许下载如下所示的文件。

    @RequestMapping(value="/downloadFile", method = RequestMethod.GET)
    @ResponseBody
    public void downloadResource(HttpServletResponse response, @RequestParam("fileName") String name) {
        downloadService.flushFile(response, name);
    }
    

    当前安全配置如下:

        @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().antMatchers("/", "/v1/**").permitAll()
        .anyRequest().rememberMe();
        http.httpBasic();
    }
    

    问题: :如何启用硬编码令牌,以便客户端javascript可以在它发送的每个请求中包含它?

    当前在调用GET服务时没有传递安全令牌。

    window.open("http://localhost:8080/DownloadMS/v1/downloadFile?fileName=abc_test");
    

    我试过在调用GET服务时使用用户名和密码,如下所示。这可能是不好的,因为在页面检查时可以清楚地看到密码(在所有浏览器中)。

    window.open("http://username:password@localhost:8080/DownloadMS/v1/downloadFile?fileName=abc_test");
    

    1 回复  |  直到 7 年前
        1
  •  1
  •   git-flo    7 年前

    根据您的用例,有多种方法可以实现基于令牌的身份验证。最常见的是 OAuth2 . 这个 spring-security-oauth2 连同 spring-security-oauth2-autoconfigure 非统组织2 合适的应用。例如,它带来了 ResourceServer AuthorizationServer . 你可以找到官方的Spring文档 here .

    here . 它充分利用了 spring-security-jwt 授权服务器 资源服务器