【问题标题】:How do I do HTTP basic auth using Struts 2/ Spring 3?如何使用 Struts 2/Spring 3 进行 HTTP 基本身份验证?
【发布时间】:2011-12-02 16:15:02
【问题描述】:

我正在编写一个 RESTful Web 服务,并且某些 API 需要用户身份授权。由于 HTTP 基本身份验证足以满足我的要求,因此我决定使用它。

我想根据存储这些凭据的 MySQL 数据库表检查我的 API 用户提供的用户凭据。

我如何使用 Struts 2/Spring 3 实际实现这一点?

【问题讨论】:

  • 我没有使用网络服务,但我相信你必须向你的网络服务消费者公开某种方法,他们可以在那里发送带有凭据的请求。所以它应该是一个简单的方法可能在您的操作类中,从服务中调用 userAuth 方法,向其传递所需的用户 ID 和密码,如果用户有效或无效,此方法应负责向您发送一个标志
  • 是的,这正是我想要做的,如果我的 API 用户通过他们的 GET/POST 方法中的参数发送用户名/密码,我知道该怎么做。我不知道如何通过直接在参数中发送凭据而不是在 HTTP 标头中的“授权”标头中进行授权,因为这更像是 Web 服务的约定。
  • 我的意思是我不知道如何检索 Authorization 标头并解析它们并检查数据库。 Struts2/Spring3中肯定有一些代码在做这个,就是不知道在哪里,怎么用。
  • 不确定对您有多大帮助,但有一个适用于 Struts 2 的 rest 插件。

标签: java web-services struts2


【解决方案1】:

您可以为此使用 spring-security。我只是按照http://syntx.co/languages-frameworks/adding-http-basic-auth-to-restful-services-in-java-and-spring/的教程做的

要对数据库进行身份验证,您可能需要创建自己的身份验证提供程序;有一个例子:Spring Security 3 database authentication with Hibernate

简而言之:

1) 在 pom.xml 中添加 spring-security:

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>       

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>   

2) 向 web.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>/*</url-pattern>
 </filter-mapping>

(您还需要 spring ContextLoaderListener,但如果您已经在使用 spring,它可能已经在您的 web.xml 中)

3) 更新你的 spring xml 配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    ...
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    ...
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <security:http auto-config='true'>
        <security:intercept-url pattern="/**" access="ROLE_USER" />
        <security:http-basic />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <!-- this is an demo example with hardcoded username and password -->
            <security:user-service>
                <security:user name="..." password="..." authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
    ....
</beans>

我的示例没有展示如何针对数据库进行身份验证:如上所述,它已经包含在 Spring Security 3 database authentication with Hibernate 中(没有尝试过,因为我现在只需要硬编码的用户名和密码)。

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 2014-09-22
    • 2011-02-11
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多