我尝试实现Spring 安全 </跨度> JAX-RS项目的基本认证。pom.xml文件
&LT;项目 xsi:schemaLocation =“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0 &gt;&LT; /性状&gt;&LT;依赖性&GT; &LT;依赖性&GT; &LT;&的groupId GT; org.springframework.security&LT; /&的groupId GT; &LT; artifactId的&GT;弹簧 安全 </跨度> -web&LT; / artifactId的&GT; &lt; version&gt; $ {org.springframework.version
这是因为版本与我添加的jar和我在spring上下文中使用的xmlns冲突。
这是我的工作代码。
web.xml中
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>JAX-WS/JAX-RS Webapp</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/security-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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> <context-param> <param-name>applicationKey</param-name> <param-value>attws</param-value> </context-param> <servlet> <servlet-name>JAXServlet</servlet-name> <display-name>JAX-WS/JAX-RS Servlet</display-name> <description>JAX-WS/JAX-RS Endpoint</description> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <init-param> <param-name>service-list-stylesheet</param-name> <param-value>servicelist.css</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JAXServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
安全的context.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd"> <security:http auto-config="true"> <!--<security:intercept-url pattern="/logs/**" access="ROLE_USER, ROLE_ADMIN"/> <security:intercept-url pattern="/logviewer.html" access="ROLE_USER, ROLE_ADMIN"/> <security:intercept-url pattern="/qr/**" access="ROLE_USER, ROLE_ADMIN"/> <!– security:intercept-url pattern="/sms/**" access="ROLE_USER, ROLE_ADMIN"/–> <security:intercept-url pattern="/services/**" access="ROLE_USER"/>--> <security:http-basic/> </security:http> <security:authentication-manager alias="authenticationManager"> <security:authentication-provider> <security:user-service> <security:user name="username1" password="password1" authorities="ROLE_USER"/> <security:user name="username2" password="password2" authorities="ROLE_USER, ROLE_ADMIN"/> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans>
CXF-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <!-- The SecureAnnotationsInterceptor honors the @RolesAllowed, @PermitAll and @DenyAll annotations --> <bean id="authorizationInterceptor" class="org.apache.cxf.interceptor.security.SecureAnnotationsInterceptor"> <property name="securedObject" ref="libraryServiceBean"/> </bean> <jaxrs:server id="libraryService" address="/library"> <!-- set the interceptor for the jaxrs:server for in-bound messages to authorize the user --> <jaxrs:inInterceptors> <ref bean="authorizationInterceptor"/> </jaxrs:inInterceptors> <jaxrs:serviceBeans> <ref bean="libraryServiceBean"/> </jaxrs:serviceBeans> </jaxrs:server> <bean id="tagServiceBean" class="att.jaxrs.server.TagService"/> <bean id="libraryServiceBean" class="att.jaxrs.server.LibraryService"/> </beans>
和依赖
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${org.springframework.version}</version> <type>jar</type> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${org.springframework.version}</version> <type>jar</type> <scope>runtime</scope> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency>
春天版 <org.springframework.version>3.2.0.RELEASE</org.springframework.version>
<org.springframework.version>3.2.0.RELEASE</org.springframework.version>