使用令牌

时间:2017-10-31 21:41:52

标签: spring spring-security oauth-2.0 jwt

我正在使用来自official spring guide的OAuth2 Spring设置 - 完整的授权服务器部分。

有两个应用程序 - 嵌入了用户资源服务器的单独授权服务器和一个客户端应用程序 - 使用JWT OAuth。

默认情况下,如果要导航到客户端应用程序的受保护资源,您将被重定向到授权服务器应用程序,您可以在其中选择要用于会话的身份验证提供程序。问题是我想支持本地登录机制。

login form

我设法引入了一个简单的登录表单,它只是从/ user资源获得的基本身份验证工作正常,除了没有重定向回到首先启动进程并且没有发出JWT令牌的资源。

通常我会使用JWT令牌重定向,但我想基本身份验证甚至不具备身份验证成功处理程序,更不用说 FeddRequestAuthenticationSuccessHandler OAuth2ClientAuthenticationProcessingFilter 似乎正在使用成功登录后。

这是我最初的想法:

@Throws(Exception::class)
    override fun configure(http: HttpSecurity) {
        // .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        http.antMatcher("/**")
                .httpBasic()
                .authenticationEntryPoint(LoginUrlAuthenticationEntryPoint("/"))
                .and()
                .authorizeRequests()
                .antMatchers("/", "/login**", "/webjars/**").permitAll()
                .antMatchers("/registration/**").permitAll()
                .antMatchers(HttpMethod.POST, "/auth/account/password/reset*/**").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .exceptionHandling()
                .and().logout()
                .logoutSuccessUrl("/").permitAll()
                // .and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .and().csrf().disable() // todo consider how to enable this only for parts of the service which is exposed to the web browser
                .addFilterBefore(createClientFilter(), BasicAuthenticationFilter::class.java)
    }

在链的最后某处

val oldRedirectUrl = HttpSessionRequestCache().getRequest(request, response).redirectUrl
        DefaultRedirectStrategy().sendRedirect(request, response, oldRedirectUrl) // all those should be in authentication success handler for basic auth

唯一的问题是,一旦用户在auth服务器(端口9998)上进行身份验证并被重定向到初始应用程序(端口9999),他就会收到以下错误:

enter image description here

他第二次这样做(当他已经过身份验证时)它运行正常。在某个地方读到这个问题可能会导致应用程序窃取其他Cookie,因此我使用

重命名了Cookie
server:
  session:
    cookie:
      name: AUTH_SESSION

配置选项。在localhost域下生成的cookie(在身份验证之后)是:

enter image description here

有趣的是AUTH_SESSION cookie在登录后会更改其值。 另外我不知道JSESSION_ID来自哪里。也许这就是问题所在?

顺便说一下,当使用formLogin auth。时,它的工作正常。

0 个答案:

没有答案
相关问题