最近,我遇到了一个非常有趣的问题,我认为这只花了我几分钟就解决了:在Windows Server 2003中使用Spring Security (当前稳定版本3.2.5 )保护Apache CXF (当前版本3.0.1 )/ JAX-RS REST服务。在嵌入式Jetty容器(当前版本9.2 )中运行的应用程序。 最后,一旦您了解了事物如何协同工作并了解了细微的内在细节,这将变得非常容易。 这篇博客文章将试图揭示这一点。

我们的示例应用程序将公开一个简单的JAX-RS / REST服务来管理人员。 但是,我们不希望所有人都这样做,因此需要HTTP基本身份验证才能访问部署在http:// localhost:8080 / api / rest / people的端点。 让我们看一下PeopleRestService类:

package com.example.rs;import javax.json.Json;
import javax.json.JsonArray;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;@Path( "/people" )
public class PeopleRestService {@Produces( { "application/json" } )@GETpublic JsonArray getPeople() {return Json.createArrayBuilder().add( Json.createObjectBuilder().add( "firstName", "Tom" ).add( "lastName", "Tommyknocker" ).add( "email", "a@b.com" ) ).build();}
}

正如您在上面的代码片段中所看到的,没有任何迹象表明该REST服务是安全的,只是几个熟悉的JAX-RS批注。

现在,让我们根据出色的Spring Security文档声明所需的安全配置。 有很多方法可以配置Spring Security,但是我们将展示其中两种:使用内存内身份验证和使用用户详细信息服务,这两种方法都基于WebSecurityConfigurerAdapter构建 。 让我们从内存中身份验证开始,因为它是最简单的一种:

package com.example.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity( securedEnabled = true )
public class InMemorySecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser( "user" ).password( "password" ).roles( "USER" ).and().withUser( "admin" ).password( "password" ).roles( "USER", "ADMIN" );}@Overrideprotected void configure( HttpSecurity http ) throws Exception {http.httpBasic().and().sessionManagement().sessionCreationPolicy( SessionCreationPolicy.STATELESS ).and().authorizeRequests().antMatchers("/**").hasRole( "USER" );}
}

在上面有该段两个用户定义: 用户与角色用户管理员用户的角色,ADMIN。 我们还通过将授权策略设置为仅允许访问角色为USER的用户来保护所有URL( / ** )。 作为应用程序配置的一部分,让我们使用@Import批注将其插入AppConfig类。

package com.example.config;import java.util.Arrays;import javax.ws.rs.ext.RuntimeDelegate;import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.provider.jsrjsonp.JsrJsonpProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Import;import com.example.rs.JaxRsApiApplication;
import com.example.rs.PeopleRestService;@Configuration
@Import( InMemorySecurityConfig.class )
public class AppConfig { @Bean( destroyMethod = "shutdown" )public SpringBus cxf() {return new SpringBus();}@Bean @DependsOn ( "cxf" )public Server jaxRsServer() {JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );factory.setServiceBeans( Arrays.< Object >asList( peopleRestService() ) );factory.setAddress( factory.getAddress() );factory.setProviders( Arrays.< Object >asList( new JsrJsonpProvider() ) );return factory.create();}@Bean public JaxRsApiApplication jaxRsApiApplication() {return new JaxRsApiApplication();}@Bean public PeopleRestService peopleRestService() {return new PeopleRestService();}
}

到目前为止,除了最有趣的部分,我们还有所有其他部分:运行嵌入式Jetty实例并创建正确的servlet映射,侦听器,传递我们创建的配置的代码。

package com.example;import java.util.EnumSet;import javax.servlet.DispatcherType;import org.apache.cxf.transport.servlet.CXFServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;import com.example.config.AppConfig;public class Starter {public static void main( final String[] args ) throws Exception {Server server = new Server( 8080 );// Register and map the dispatcher servletfinal ServletHolder servletHolder = new ServletHolder( new CXFServlet() );final ServletContextHandler context = new ServletContextHandler();   context.setContextPath( "/" );context.addServlet( servletHolder, "/rest/*" );  context.addEventListener( new ContextLoaderListener() );context.setInitParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );context.setInitParameter( "contextConfigLocation", AppConfig.class.getName() );// Add Spring Security Filter by the namecontext.addFilter(new FilterHolder( new DelegatingFilterProxy( "springSecurityFilterChain" ) ), "/*", EnumSet.allOf( DispatcherType.class ));server.setHandler( context );server.start();server.join(); }
}

除了过滤器部分,大多数代码不需要任何说明。 这就是我所说的微妙的固有细节: DelegatingFilterProxy应该配置为过滤器名称,该名称必须完全是springSecurityFilterChain ,因为Spring Security会为其命名。 这样,我们配置的安全规则将适用于任何JAX-RS服务调用(安全过滤器在Apache CXF servlet之前执行),需要完全认证。 让我们通过构建和运行项目来快速检查:

mvn clean package
java -jar target/jax-rs-2.0-spring-security-0.0.1-SNAPSHOT.jar

在不提供用户名和密码的情况下发出HTTP GET调用不会成功,并返回HTTP 状态代码401 。

> curl -i http://localhost:8080/rest/api/peopleHTTP/1.1 401 Full authentication is required to access this resource
WWW-Authenticate: Basic realm="Realm"
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html; charset=ISO-8859-1
Content-Length: 339
Server: Jetty(9.2.2.v20140723)

提供的用户名和密码相同的HTTP GET调用返回成功的响应(服务器生成一些JSON)。

> curl -i -u user:password http://localhost:8080/rest/api/peopleHTTP/1.1 200 OK
Date: Sun, 28 Sep 2014 20:07:35 GMT
Content-Type: application/json
Content-Length: 65
Server: Jetty(9.2.2.v20140723)[{"firstName":"Tom","lastName":"Tommyknocker","email":"a@b.com"}]

太好了,它就像一个魅力! 事实证明,这确实非常容易。 同样,如前所述,可以使用用户详细信息服务代替内存中身份验证,这是一个示例,该示例说明了如何进行:

package com.example.config;import java.util.Arrays;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class UserDetailsSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService( userDetailsService() );}@Beanpublic UserDetailsService userDetailsService() {return new UserDetailsService() {@Overridepublic UserDetails loadUserByUsername( final String username ) throws UsernameNotFoundException {if( username.equals( "admin" ) ) {return new User( username, "password", true, true, true, true,Arrays.asList(new SimpleGrantedAuthority( "ROLE_USER" ),new SimpleGrantedAuthority( "ROLE_ADMIN" )));} else if ( username.equals( "user" ) ) {return new User( username, "password", true, true, true, true,Arrays.asList(new SimpleGrantedAuthority( "ROLE_USER" )));} return null;}};}@Overrideprotected void configure( HttpSecurity http ) throws Exception {http.httpBasic().and().sessionManagement().sessionCreationPolicy( SessionCreationPolicy.STATELESS ).and().authorizeRequests().antMatchers("/**").hasRole( "USER" );}
}

AppConfig类中的@Import(InMemorySecurityConfig.class)替换为@Import( UserDetailsS​​ecurityConfig.class)会得到相同的结果,因为两个安全配置都定义了相同的用户及其角色集。

我希望,此博客文章可以节省您一些时间,并为您提供一个良好的起点,因为Apache CXF和Spring Security在Jetty的保护下相处得很好!

  • 完整的源代码可在GitHub上获得 。

翻译自: https://www.javacodegeeks.com/2014/09/embedded-jetty-and-apache-cxf-secure-rest-services-with-spring-security.html

嵌入式Jetty和Apache CXF:借助Spring Security来保护REST服务相关推荐

  1. jetty嵌入式容器_嵌入式Jetty和Apache CXF:借助Spring Security来保护REST服务

    jetty嵌入式容器 最近,我遇到了一个非常有趣的问题,我认为这只需要几分钟就可以解决:在Linux中使用Spring Security (当前稳定版本3.2.5 )保护Apache CXF (当前版 ...

  2. 使用Apache cxf 和Spring在Tomcat下发布Webservice指南

    转载 http://blog.csdn.net/zhangzhaokun/article/details/4750021 最近学习了如何使用apache cxf和Spring发布webservice, ...

  3. 《深入理解 Spring Cloud 与微服务构建》第十七章 使用 Spring Cloud OAuth2 保护微服务系统

    <深入理解 Spring Cloud 与微服务构建>第十七章 使用 Spring Cloud OAuth2 保护微服务系统 文章目录 <深入理解 Spring Cloud 与微服务构 ...

  4. Apache Shiro和Spring Security的详细对比

    参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...

  5. 使用Spring Boot 2.0的Spring Security:保护端点

    到目前为止,在以前的文章中,我们已经使用默认的spring安全配置对端点和控制器进行了安全保护. 当Spring Security在类路径上时,默认情况下自动配置会保护所有端点. 当涉及到复杂的应用程 ...

  6. jax-ws cxf_走向REST:在Spring和JAX-RS(Apache CXF)中嵌入Jetty

    jax-ws cxf 对于服务器核心Java开发人员来说,向世界"展示"的唯一方法是使用API​​. 今天的帖子都是关于JAX-RS的 :使用Java编写和公开RESTful服务. ...

  7. 走向REST:在Spring和JAX-RS(Apache CXF)中嵌入Jetty

    对于服务器核心Java开发人员而言,向世界"展示"的唯一方法是使用API​​. 今天的帖子都是关于JAX-RS的 :使用Java编写和公开RESTful服务. 但是,我们不会使用涉 ...

  8. Spring Security 和 Apache Shiro

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 前言 web应用达到生产需要就必须有安全控制.java web领域经常提及的两大开源框架主要 ...

  9. Spring Security中文文档

    Spring Security中文文档 来源:https://www.springcloud.cc/spring-security.html#overall-architecture 作者 Ben A ...

最新文章

  1. 计算机网络犯罪特点,计算机网络犯罪有哪些特征
  2. python3 异步 非阻塞 IO多路复用 select poll epoll 使用
  3. jquery设置复选框为只读_checkbox设置复选框的只读效果不让用户勾选
  4. ASP.NET WebForm中用async/await实现异步
  5. python 高斯烟羽模型_GPR(高斯过程回归)详细推导
  6. 《TensorFlow 机器学习方案手册》(附 pdf 和完整代码)
  7. (Docker实战) 第2篇:Centos7 拉取和部署Gitlab
  8. 基于FPGA的EEPROM读写(IIIC 接口协议)
  9. python命令行解析库——argparse库的使用
  10. C#获取C# DLL中的指定接口的所有实现实例 - qq_19759475的博客 - CSDN博客
  11. J-Link驱动下载(含历史版本)
  12. c语言99乘法表流程图表,C语言做99乘法表.doc
  13. 山东金税盘如何跨月作废增值税普通发票
  14. 婚姻法新解释引女方净身出户担忧 或导致房产加名热
  15. 伴随着三维全息投影技术的升级,物理屏幕将彻底消失
  16. c语言延时0.5s程序,C语言延时程序(ms,us)
  17. 请教PCI卡的DMA方式问题
  18. 基于Android的MediaPlayer的音乐播放器的封装
  19. Python计算经济距离权重矩阵
  20. HDFS的读写流程步骤(附图文解析)

热门文章

  1. Android中SlidingDrawer开发报错You need to use a Theme.AppCompat theme (or descendant) with this activity.
  2. 19年8月 字母哥 第六章 生命周期内的拦截过滤与监听 用热点公司网不行
  3. 在C语言的函数定义中 如果不需要返回结果,在C语言的函数定义中,如果不需要返回结果,就可以省略return语句...
  4. 最新开发android版本,Android版本检测升级
  5. 转:在eclipse中搭建maven工程(第二种方法)
  6. Java的值传递解析
  7. maven配置junit5_JUnit 5和Selenium –改善项目配置
  8. jakarta ee_Jakarta EE中的规范范围
  9. stripe pay_J2Pay –简介
  10. 琥珀ai_琥珀项目:Java的未来暴露