Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 春季安全纪念&;RESTful API_Spring_Rest_Authentication_Spring Security - Fatal编程技术网

Spring 春季安全纪念&;RESTful API

Spring 春季安全纪念&;RESTful API,spring,rest,authentication,spring-security,Spring,Rest,Authentication,Spring Security,我正试图实现一个API 基本流程是: 移动应用嵌入用户登录的web视图(普通web应用安全) 登录时,webapp返回一个带有令牌的安全cookie 移动应用程序在将来的所有API请求中使用令牌 因此,我试图通过登录表单保护我的webapp,以允许正常的登录流(使用Spring安全性),然后根据请求头中传递的令牌对/api/**的所有请求进行身份验证 我最初是通过两个web配置来实现这一点的——一个用于正常的webapp安全性: @Override protected void config

我正试图实现一个API

基本流程是:

  • 移动应用嵌入用户登录的web视图(普通web应用安全)
  • 登录时,webapp返回一个带有令牌的安全cookie
  • 移动应用程序在将来的所有API请求中使用令牌
因此,我试图通过登录表单保护我的webapp,以允许正常的登录流(使用Spring安全性),然后根据请求头中传递的令牌对/api/**的所有请求进行身份验证

我最初是通过两个web配置来实现这一点的——一个用于正常的webapp安全性:

@Override protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .disable()
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/sign-in").permitAll()
                .antMatchers("/success").authenticated()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/")
                .loginProcessingUrl("/loginprocess")
                .failureUrl("/sign-in?loginFailure=true")
                .permitAll();
    }
上面只定义了标准的spring安全身份验证(从DB获取用户详细信息的自定义userDetailsService)以及登录和成功页面的身份验证

然后是另一个(两个文件只是为了清晰/易于阅读)用于API身份验证:

@Override protected void configure(HttpSecurity http) throws Exception {
    http
        .antMatcher("/api/**")
        .csrf()
            .disable()
        .authorizeRequests().anyRequest().authenticated().and()
        .addFilterBefore(authenticationTokenFilter(), BasicAuthenticationFilter.class )
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint());
}
上面有一个预验证过滤器,它从请求(令牌等)中获取相关的HTTP头,并将它们添加到安全上下文中-然后自定义验证提供程序验证令牌/用户详细信息

一切都很好——然而,感觉就像我在重新发明一堆东西。查看了Spring Security的Memberme功能后,看起来他们已经处理了很多这方面的问题—在登录时,它返回一个带有令牌的cookie,然后可以在将来的请求中用于自动登录。发件人:


唯一的问题是,我希望在将来的请求中将令牌作为头参数,而不是cookie。我已经查看了上述类的源代码,TokenBasedMemberMeservices类
autoLogin()
方法显式检查cookie。代码还使用MD5作为散列的默认值

我的问题是:

  • 是否有一个标准的Spring类可以处理RememberMe 来自请求头而不是cookie的功能
  • 有没有比MD5更好的哈希算法?(会吗 轻松切换,无需覆盖。或者,更好的是,我来了 对面看起来像一个 更好的令牌生成服务(SHA512和其他密钥信息), 有没有使用它来创建/验证 安全令牌

  • 我不希望必须扩展所有这些类,因为一堆核心内容都是一样的,而且为了更改令牌的来源和哈希算法而必须扩展它们似乎有些过火。

    好的,环顾一下,似乎没有实现请求头的RememberMe功能的核心类——然而,通过阅读Spring Security MemberMe源代码,扩展上述类来查看请求头实际上非常简单


    详细信息都在这里:但基本上只是像平常一样使用Memberme,然后扩展TokenBasedMemberMeservices并重写extractCookie方法,只从标头而不是cookie中获取令牌(可能有点黑客,扩展名为extractCookie的方法以获取请求标头,但它可以工作)

    非常有帮助,我也在考虑采用同样的方法。您是否有幸利用类似于KeyBasedPersistenceTokenService的工具生成令牌?没有,我没有考虑使用它-我只是坚持使用标准的RememberMe令牌生成(因为它对于浏览器中的“记住我”功能来说已经足够好了,所以我决定不投资更改它。对httpBasic()有什么想法吗方法
    <bean id="rememberMeFilter" class=
     "org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
      <property name="rememberMeServices" ref="rememberMeServices"/>
      <property name="authenticationManager" ref="theAuthenticationManager" />
    </bean>
    
    <bean id="rememberMeServices" class=
     "org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
      <property name="userDetailsService" ref="myUserDetailsService"/>
      <property name="key" value="springRocks"/>
    </bean>
    
    <bean id="rememberMeAuthenticationProvider" class=
     "org.springframework.security.authentication.rememberme.RememberMeAuthenticationProvider">
      <property name="key" value="springRocks"/>
    </bean>