哈尔滨网站优化流程,做高效能的父母网站,国外html 网站,app免费下载安装在我之前的博客文章“ 检查REST API是否有效的快速方法–从清单文件中获取GET详细信息”中 #xff0c;我展示了如何开发REST资源以轻松检查开发的REST API是否可用。 在本文中#xff0c;我将介绍如何使用Spring Security和基本身份验证来保护此资源的安全性– “在HTTP事务… 在我之前的博客文章“ 检查REST API是否有效的快速方法–从清单文件中获取GET详细信息”中 我展示了如何开发REST资源以轻松检查开发的REST API是否可用。 在本文中我将介绍如何使用Spring Security和基本身份验证来保护此资源的安全性– “在HTTP事务的上下文中基本访问身份验证是HTTP用户代理在发出请求时提供用户名和密码的方法。” 此处介绍的受保护的REST资源是更大项目的一部分在教程中进行了广泛介绍-REST API设计和Java以及Jersey和Spring的实现 使用的软件 泽西岛JAX-RS实现2.14 Spring4.1.4 Spring Security 3.2.5 Maven 3.1.1 JDK 7 Spring安全配置 图书馆 为了通过基本身份验证保护REST服务在类路径中需要以下Spring安全性库。 因为我使用的是Maven所以它们在pom.xml中被列为Maven依赖项 Spring安全性库 !-- Spring security --
dependencygroupIdorg.springframework.security/groupIdartifactIdspring-security-core/artifactIdversion${spring.security.version}/version
/dependency
dependencygroupIdorg.springframework.security/groupIdartifactIdspring-security-web/artifactIdversion${spring.security.version}/version
/dependency
dependencygroupIdorg.springframework.security/groupIdartifactIdspring-security-config/artifactIdversion${spring.security.version}/version
/dependency安全应用程序上下文 Spring安全配置 ?xml version1.0 encodingUTF-8?
beans:beans xmlnshttp://www.springframework.org/schema/securityxmlns:beanshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:securityhttp://www.springframework.org/schema/security xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd!-- Stateless RESTful services use BASIC authentication --security:http create-sessionstateless pattern/manifest/**security:intercept-url pattern/** accessROLE_REST/security:http-basic//security:httpsecurity:authentication-managersecurity:authentication-providersecurity:user-servicesecurity:user namerest passwordrest authoritiesROLE_REST//security:user-service/security:authentication-provider/security:authentication-manager/beans:beans 如您所见 “剩余”用户和角色在内存中定义。 这些定义在元素security:user-service及其子元素security:user 。 这样可以确保只有具有ROLE_REST角色的用户才能访问 具有内存设置的身份验证管理器 security:authentication-managersecurity:authentication-providersecurity:user-servicesecurity:user namerest passwordrest authoritiesROLE_REST//security:user-service/security:authentication-provider
/security:authentication-manager 下一步是保护/manifest/* URL仅允许访问新定义的休息用户 通过基于角色的访问保护URL security:http create-sessionstateless pattern/manifest/**security:intercept-url pattern/** accessROLE_REST/security:http-basic/
/security:http 在我们的应用程序中通过security:http-basic/行启用了基本HTTP身份验证。 注意 您不能在applicationContext.xml文件中定义Spring Security的安全性约束因为它们需要与Servlet侦听器和过滤器链一起加载。 它们需要位于由Servlet侦听器定义的适当的WebApplicationContext中而不是与Servlet相关的侦听器中。 这是因为DelegatingFilterProxy将查找ContextLoaderListener加载的ServletContext中定义的根应用程序上下文。 如果仅定义applicationContext.xml因为过滤器首先加载则在servlet之前加载筛选器将无法找到任何应用程序上下文因此将无法正确加载。 Web.xml 现在扩展contextConfigLocation上下文参数以了解新的spring安全配置文件security-context.xml web.xml –上下文参数扩展 context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring/applicationContext.xmlclasspath:spring/security-context.xml/param-value
/context-param 仅针对与清单相关的URL钩住Spring安全性 钩住Spring安全 servletservlet-namejersey-servlet/servlet-nameservlet-classorg.glassfish.jersey.servlet.ServletContainer/servlet-classinit-paramparam-namejavax.ws.rs.Application/param-nameparam-valueorg.codingpedia.demo.rest.RestDemoJaxRsApplication/param-value /init-param load-on-startup1/load-on-startup
/servletservlet-mappingservlet-namejersey-servlet/servlet-nameurl-pattern/*/url-pattern
/servlet-mapping!--Hook into spring security--
filterfilter-namespringSecurityFilterChain/filter-namefilter-classorg.springframework.web.filter.DelegatingFilterProxy/filter-class
/filterfilter-mappingfilter-namespringSecurityFilterChain/filter-nameurl-pattern/manifest/*/url-pattern
/filter-mapping Spring安全过滤器链需要激活。 测试中 浏览器 如果您通过浏览器访问受保护的位置则会显示一个标准HTTP身份验证弹出窗口询问身份验证详细信息 放置休息/休息 您应该会收到JSON响应。 SoapUI 通过soapUI使用基本身份验证测试安全的REST相当容易。 观看以下视频以了解更多信息 请求 使用基本身份验证请求资源 GET http://localhost:8888/demo-rest-jersey-spring/manifest HTTP/1.1
Accept-Encoding: gzip,deflate
Accept: application/json
Host: localhost:8888
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Authorization: Basic cmVzdDpyZXN0 请注意Authorization标头其结构如下 用户名和密码合并为字符串“ usernamepassword” 然后使用Base64的RFC2045-MIME变体对结果字符串进行编码但不限于76个字符/行 然后将授权方法和一个空格即“ Basic”放置在编码字符串之前。 响应 回应–清单明细 HTTP/1.1 200 OK
Date: Tue, 03 Feb 2015 15:47:32 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Headers: X-Requested-With, Content-Type, X-Codingpedia
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 196
Server: Jetty(9.2.6.v20141205){Implementation-Title:DemoRestWS,Implementation-Version:0.0.1-SNAPSHOT,Implementation-Vendor-Id:org.codingpedia,Built-By:Matei1.Adrian,Build-Jdk:1.7.0_40,Manifest-Version:1.0,Created-By:Apache Maven 3.1.1,Specification-Title:DemoRestWS,Specification-Version:0.0.1-SNAPSHOT}摘要 好吧就是这样。 Spring Security是一个非常强大的框架带有大量的配置选项。 在本文中我仅演示了其中之一即如何使用基本身份验证来保护REST资源。 当然在更现实的情况下您可以将用户和角色存储在LDAP目录或数据库中…… 注意如果您决定使用基本身份验证来保护REST资源请确保通过HTTPS调用它们。 如今保护REST资源安全的首选方法是使用OAuth 。 有关更多信息请参见稍后的文章。 翻译自: https://www.javacodegeeks.com/2015/02/secure-jersey-rest-services-spring-security-basic-authentication.html