【问题标题】:Polymer UI with Spring Rest API - authentication issue带有 Spring Rest API 的 Polymer UI - 身份验证问题
【发布时间】:2018-06-29 13:44:00
【问题描述】:

我正在尝试使用针对 LDAP 的基于表单的身份验证来保护对某些 REST API 的访问。这部分实际上工作正常: - 我在http://localhost:1304/login 的默认页面提供的表单中输入用户和密码,然后我可以访问所有其他 API:http://localhost:1304/userhttp://localhost:1304/api/v1/search/draft

问题是我正在尝试使用来自 Polymer 前端的 http://localhost:1304/api/v1/search/draft 等 API,该前端在 localhost:8081 的节点服务器上单独启动。我设法摆脱了所有 CORS 问题,所以我认为与这些无关。我正在使用 ajax-form 发布用户名&密码&提交,我可以在 spring 应用程序日志中看到身份验证成功。但是,当我的 UI 尝试对该 /api/v1/search/draft API 执行 GET 时,它会失败,并且调用会在后台重定向到 /login(或者当我使用自定义 authenticationEntryPoint 时返回 401)。但是,如果我使用http://localhost:1304/login 登录,那么我的 UI 页面能够执行 API GET。

春天我有:

 protected void configure(HttpSecurity http) throws Exception {
    http
    .cors()
    .and()
    .authorizeRequests()
    .antMatchers("/login", "/","/src/**","/bower_components/**", "/oauth/authorize", "/oauth/confirm_access", "/logout", "/oauth/revoke-token","/oauth/check_token").permitAll()
    .anyRequest().authenticated()
    .and()
    .formLogin()
        .failureHandler(authFailureHandler)
    .permitAll()
    .and()
    .logout()                                    
    .permitAll()
    .and()
    .rememberMe()
    .and()
    .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
    .and()
    .exceptionHandling()
    .and()
    .csrf().disable()
    ; }

    @Bean
    CorsConfigurationSource corsConfigurationSource()
    {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:8081"));
        configuration.setAllowedMethods(Arrays.asList("HEAD", "GET","POST","OPTIONS"));
        configuration.setAllowCredentials(true);

        configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
        //configuration.setAllowedHeaders(Arrays.asList("Access-Control-Allow-Credentials"));
        configuration.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

/api/v1/search/draft 使用 Iron-ajax 调用:

<iron-ajax id="get_drafts"  handle-as="json" last-response="{{drafts}}" on-response="_onDraftsResponse" debounce-duration="900" with-credentials="true"></iron-ajax>

对于我的聚合物 UI 的身份验证,标题是这样的:

**GENERAL**
Request URL:http://localhost:1304/login
Request Method:POST
Status Code:302 
Remote Address:[::1]:1304
Referrer Policy:no-referrer-when-downgrade
**Response Headers**
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8081
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Sat, 20 Jan 2018 11:43:25 GMT
Expires:0
Location:http://localhost:1304/
Pragma:no-cache
Set-Cookie:JSESSIONID=DBCAF4C1FD5D1C1655EFF873B6D556EE; Path=/; HttpOnly
Vary:Origin
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
**Request Headers**
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7
Connection:keep-alive
Content-Length:96
Content-Type:application/x-www-form-urlencoded
Host:localhost:1304
Origin:http://localhost:8081
Referer:http://localhost:8081/overview
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

使用请求标头中的不同 sessionID 对 /draft 的后续 API 调用

Cookie:JSESSIONID=309D6615798B57247756C33808D57559

对于使用默认 /login 表单的身份验证,我有:

**GENERAL**
Request URL:http://localhost:1304/login
Request Method:POST
Status Code:302 
Remote Address:[::1]:1304
Referrer Policy:no-referrer-when-downgrade
**Response Headers**
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:0
Date:Sat, 20 Jan 2018 11:47:30 GMT
Expires:0
Location:http://localhost:1304/
Pragma:no-cache
Set-Cookie:JSESSIONID=330CDE874819AA32E134A41EFB21B8E1; Path=/; HttpOnly
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
**Request Headers**

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:ro-RO,ro;q=0.9,en-US;q=0.8,en;q=0.7
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:46
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=309D6615798B57247756C33808D57559
Host:localhost:1304
Origin:http://localhost:1304
Referer:http://localhost:1304/login?logout
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36

从现在开始使用新的所有其他调用

JSESSIONID=330CDE874819AA32E134A41EFB21B8E1

我觉得我真的很接近解决方案了。我错过了什么? 谢谢

【问题讨论】:

    标签: ajax spring authentication polymer jsessionid


    【解决方案1】:

    问题出在我使用的 ajax 表单上。更改为简单的表单解决了身份验证问题,但现在从 localhost:8081(UI 在哪里)我被重定向到 localhost:1304(从中调用登录 API)。我想我需要去spring code来解决这个问题。

    【讨论】:

      猜你喜欢
      • 2013-06-16
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 2016-11-02
      • 2015-02-11
      • 1970-01-01
      相关资源
      最近更新 更多