Java 如何使用Struts 2/Spring 3进行HTTP基本身份验证?
我正在编写一个RESTful web服务,某些API需要用户身份授权。由于HTTP basic auth正好满足我的需求,我决定使用它 我想对照存储这些凭证的MySQL数据库表检查API用户提供的用户凭证Java 如何使用Struts 2/Spring 3进行HTTP基本身份验证?,java,web-services,struts2,Java,Web Services,Struts2,我正在编写一个RESTful web服务,某些API需要用户身份授权。由于HTTP basic auth正好满足我的需求,我决定使用它 我想对照存储这些凭证的MySQL数据库表检查API用户提供的用户凭证 如何使用Struts 2/Spring 3实现这一点?您可以使用Spring安全性来实现这一点。我就是这么做的,按照 要对数据库进行身份验证,您可能需要创建自己的身份验证提供程序;例如: 简言之: 1)在pom.xml中添加spring安全性: org.springframework.se
如何使用Struts 2/Spring 3实现这一点?您可以使用Spring安全性来实现这一点。我就是这么做的,按照 要对数据库进行身份验证,您可能需要创建自己的身份验证提供程序;例如: 简言之: 1)在pom.xml中添加spring安全性:
org.springframework.security
(没有尝试,因为我现在只需要硬编码的用户名和密码).我没有使用web服务进行exp,但我相信您必须向web服务消费者公开某种方法,他们可以使用这些凭据发送请求。因此,这应该是一个简单的方法,可能是在您的操作类中,从服务调用userAuth方法,并向其传递所需的用户ID和密码,并且此方法应该是如果用户有效或无效,负责向您发回一个标志是的,这正是我想要做的,如果我的API用户通过GET/POST方法中的参数发送用户名/密码,我知道如何做。我不知道如何进行授权,不是直接在参数中发送凭证,而是在HTTP头中的“授权”头中发送凭证,因为这更像是web服务的一种约定。我的意思是我不知道如何检索授权
头并解析它们并对照数据库进行检查。Struts2/Spring3中一定有一些代码在做这件事,我只是不知道它在哪里以及如何使用它们。不确定它能帮你多少忙,但有一个rest插件可以与Struts2一起使用。
<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>
<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>
<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>