gpt4 book ai didi

web-services - RESTful webservice + Spring token 认证

转载 作者:行者123 更新时间:2023-12-01 05:23:08 25 4
gpt4 key购买 nike

我对 Spring 不太熟悉,但我已经阅读了一些文章和操作方法。

所需业务为:

  • 典型的客户端-服务器架构:移动客户端和服务器端的 RESTful 服务
  • 客户端登录移动应用程序有不同的选择:应用程序登录和 facebook 登录
  • 必须保护服务器端的所有 RESTful 服务免受未经授权的用户的攻击

  • 我的职责是开发 RESTful 服务。我非常擅长应用服务器、Java EE、JMS、JDBC、分布式事务 (XA),但我不太擅长安全性 :(

    我用 Spring 开发了一些 STATELESS RESTful web 服务。这些服务不 protected ,因此每个人都可以使用它们。

    例如:
  • http://...../api/country/{**user_id**}
  • http://...../api/country/{**user_id**},{country_id}
  • ...

  • 我的每个网络服务都有一个 用户 ID 输入参数,因为我需要确定哪个用户进行了服务器调用。网络服务的结果取决于用户。当然,这绝对是正常的。

    现在,我必须开发一些新东西,因为我必须保护这些 Web 服务免受未经授权的用户的攻击。

    我的想法是:

    (*) 我将像这样创建两个新的 Web 服务:

    applicationLogin(字符串用户名,字符串密码)[/INDENT]

    facebookLogin(字符串访问 token )
  • http://...../api/login/{username}, {password}
  • http://...../api/login/{facebook access token}

  • (*) 我必须保护我的网络服务免受未经授权的用户的侵害

    用户登录过程可能如下所示:
    (1) 用户在其移动设备上填写用户名和密码字段
    (2)点击应用程序登录按钮
    (3) 移动应用程序向 http://...../api/login/{username}, {password} 发起服务器调用公共(public)服务
    (4) 如果用户名和密码正确,我将生成一个 token (带有过期日期信息的长字符串),并将用户名和 token 字符串放入 HTTP header 的答案中
    (5) 之后,所有客户端在进行 Web 服务调用时必须将这两个参数(用户名和 token )发送回服务器。

    在服务器端,我可以从 HTTP 请求中读取用户名,以便删除 用户 ID 来自所有 Web 服务签名的参数。

    我正在尝试在 Spring 中实现这个过程。我想我需要使用 PRE_AUTH_FILTER 来自 Spring 安全模块。但是不知道我的想法好不好?

    我做到了:

    网页xml
    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/api/country/*</url-pattern>
    </filter-mapping>

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext-security.xml, /WEB-INF/applicationContext.xml</param-value>
    </context-param>

    applicationContext-security.xml
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:security="http://www.springframework.org/schema/security"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <security:http use-expressions="true" create-session="stateless" auto-config="false" entry-point-ref="authenticationEntryPoint">
    <security:intercept-url pattern="/api/login/*" access="permitAll"/>
    <security:intercept-url pattern="/api/country/*" access="isAuthenticated()" />
    <security:custom-filter position="PRE_AUTH_FILTER" ref="authenticationTokenProcessingFilter" />
    </security:http>

    <bean id="authenticationEntryPoint" class="com.samples.spring.auth.ForbiddenAuthenticationEntryPoint" />

    <bean id="userDetailsServiceImpl" class="com.samples.spring.auth.UserDetailsServiceImpl" />

    <bean id="preAuthenticationProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">

    <property name="preAuthenticatedUserDetailsService" ref="userDetailsServiceImpl" />
    </bean>

    <bean id="authenticationTokenProcessingFilter" class="com.samples.spring.auth.AuthenticationFilter">

    <property name="authenticationManager" ref="appControlAuthenticationManager" />
    </bean>

    <security:authentication-manager alias="appControlAuthenticationManager">
    <security:authentication-provider ref="preAuthenticationProvider" />
    </security:authentication-manager>

    </beans>

    您如何看待我的登录过程?这是开始实现 token 处理方法的好方法吗?

    最佳答案

    除了使用 Spring 安全性之外,Spring 还有另一个名为 Spring Social 的模块。 .我认为这肯定会帮助您并减少开发工作。它还允许您使用其他社交网络(如 Twitter、linkedin)连接到您的应用程序。也结帐Spring Mobile

    关于web-services - RESTful webservice + Spring token 认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15449375/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com