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

jetty嵌入式容器

最近,我遇到了一个非常有趣的问题,我认为这只需要几分钟就可以解决:在Linux中使用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嵌入式容器

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/339774.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python逻辑运算符的使用_总结Python中逻辑运算符的使用

下表列出了所有python语言支持的逻辑运算符。假设变量a持有10和变量b持有20&#xff0c;则&#xff1a;示例:试试下面的例子就明白了所有的python编程语言提供了逻辑运算符&#xff1a;#!/usr/bin/pythona 10b 20c 0if ( a and b ):print "line 1 - a and b are true&q…

如何使用用户数据脚本在EC2实例上安装Apache Web Server

你好朋友&#xff0c; 在本教程中&#xff0c;我们将看到如何使用用户数据脚本在EC2实例上安装Apache Web Server。 在我以前的教程之一中&#xff0c;我已经解释了如何使用AWS控制台启动EC2实例。如果您还没有完成该操作&#xff0c;我建议您先进行一下操作。 首先&#xf…

java 常量接口_java接口定义常量研究

背景和同事讨论到&#xff0c;在接口中定义常量的问题&#xff0c;引发了争论&#xff0c;即&#xff0c;假如在接口中定义变量&#xff0c;是否需要用static来限定的问题&#xff0c;或者说用static和不用static会有什么区别。引论package spring.interfaceTest;public interf…

spring 多租户_使用Spring Security的多租户应用程序的无状态会话

spring 多租户从前&#xff0c; 我发表了一篇文章&#xff0c;解释了构建无状态会话的原理 。 巧合的是&#xff0c;我们再次为多租户应用程序执行同一任务。 这次&#xff0c;我们将解决方案集成到Spring Security框架中&#xff0c;而不是自己构建身份验证机制。 本文将解释…

java api 1.6 下载_Java JDK API

JDKJavaDevelopmentKit是SunMicrosystems针对Java开发员的产品。自从Java推出以来&#xff0c;JDK已经成为使用最广泛的JavaSDK。JDK是整个Java的核心&#xff0c;包括了Java运行环境。相关软件软件大小版本说明下载地址jdk(Java Development Kit)是Sun Microsystems针对java开…

Java / Cloud:如何快速创建支持Kubernetes的REST微服务

可以肯定地说&#xff0c;如今微服务与云的结合风靡一时。 微服务的开发比以往任何时候都多&#xff0c;从而导致应用程序部署数量增加。 在过去的十年中&#xff0c;开发了诸如Docker和Kubernetes之类的容器化和编排工具&#xff0c;从而使微服务模式真正易于采用。 本文将教…

java考试安徽工业大学_2011~2012《Java语言程序设计》试卷A及答案(安徽工业大学)...

《2011~2012《Java语言程序设计》试卷A及答案(安徽工业大学)》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《2011~2012《Java语言程序设计》试卷A及答案(安徽工业大学)(6页珍藏版)》请在人人文库网上搜索。1、装 订 线 安 徽 工 业 大 学 试 题 纸(一)20112012学年第…

jsr303自定义验证_JSR 310新日期/时间API的自定义JSR 303 Bean验证约束

jsr303自定义验证借助JSR 310&#xff0c;Java 8终于为我们带来了不错的日期和时间API。 对于仍在使用Java 7的那些人&#xff08;就像我目前在我的当前项目中一样&#xff09;&#xff0c;有很好的反向移植&#xff0c;请访问www.threeten.org了解更多详细信息。 但是&#xf…

java spring注入 静态方法_JAVA静态方法中如何使用spring@Value进行注入的成员变量...

背景&#xff1a;一个旧项目原本集成有spring-session&#xff0c;现需要临时添加缓存的操作&#xff0c;需要复用spring-session中的一些缓存配置。实现方法&#xff1a;一、类上添加注解Component二、定义静态成员变量private static String redisUrl;private static String …

使用Testcontainers和PostgreSQL,MySQL或MariaDB的Spring Boot测试

Testcontainers是一个Java库&#xff0c;可轻松将Docker容器集成到JUnit测试中。 在Containerized World中 &#xff0c;将测试配置与嵌入式数据库和服务复杂化几乎没有意义。 而是使用在Docker中运行您的服务&#xff0c;并让Testcontainers为您管理此服务。 在此博客文章中&…

使用Spring Boot和Project Reactor处理SQS消息-第2部分

这是我关于使用Spring Boot和Project Reactor有效处理SQS消息的博客文章的后续文章 我在第一部分中列出了一些方法上的差距。 1.处理SQS客户端调用中的失败 2.该方法一次只能处理来自SQS的一条消息&#xff0c;如何并行化 3.它不处理错误&#xff0c;管道中的任何错误都会中…

java爬虫jsoup_Java爬虫之利用Jsoup自制简单的搜索引擎

内容导读在上述代码中&#xff0c;url为输入词条(暂时仅限于英文)&#xff0c;进入while循环可一直搜索&#xff0c;当输入为’exit’时退出。contentText为该词条的百度百科简介的网页形式&#xff0c;通过正则表达式将其中的文字提取出来。代码虽然简洁&#xff0c;但是功能还…

shader weaver_具有自定义汇编程序,Weaver和运行时的可插拔知识

shader weaver作为贝叶斯工作的一部分&#xff0c;我对Kie进行了很多重构&#xff0c;使其具有清晰的扩展点。 我想确保可以完成贝叶斯系统的所有工作部件&#xff0c;而无需在现有内核中添加任何代码。 因此&#xff0c;现在每种知识类型都可以拥有自己的包&#xff0c;汇编器…

matplotlib的默认字体_浅谈matplotlib默认字体设置探索

控制默认字体的设置根据官方文档https://matplotlib.org/tutorials/text/text_props.html#default-font可知&#xff1a;The base default font is controlled by a set of rcParams默认字体是由一组rcParams控制的。rcParamusage‘font.family"List of either names of f…

如何使用Apache Camel,Quarkus和GraalVM快速运行100个骆驼

今天&#xff0c;我继续在youtube上练习&#xff0c;并录制了10分钟的视频&#xff0c;介绍了如何创建一个新的Camel and Quarkus项目&#xff0c;该项目包括Rest和HTTP服务以及开箱即用的健康检查和指标。 然后比较以JVM模式运行示例与使用GraalVM编译的本机内存的使用情况。…

java空心菱形_java 空心菱形

分为两部分&#xff0c;先打印前四行&#xff0c;再打印后三行&#xff0c;int n 4;    //设初始值为4for(int i0;ifor(int j0;jSystem.out.print(" ");}for(int k0;kif(k0||k2*i) {    //打印前四行的*&#xff0c;中间部分输出空格System.out.print(&quo…

java接口版本控制_为什么要在Java中控制类和接口的可见性

java接口版本控制维护是软件开发的重要方面之一&#xff0c;并且经验证明&#xff0c;保持较低组件可视性的软件比暴露更多组件的软件更易于维护。 您不会在一开始就意识到它&#xff0c;但是在重新设计应用程序时会严重错过它。 由于保持向后兼容性是许多应用程序的“必须具备…

遮掩java_css之图片下方定位遮掩层

需要的效果如图&#xff0c;图片下方加个遮掩层&#xff1a;html&#xff1a;css&#xff1a;.listContent>div{width:300px;height: 300px;float: left;margin-top: 20px;margin-left: 20px;position:relative;}.mask{width:300px;height: 40px;background-color:#FFCCCC;p…

使用JDK的密码流的加密怪癖(以及如何做)

在我们的日常工作中&#xff0c;我们经常遇到经常性的主题&#xff0c;即将数据&#xff08;例如文件&#xff09;从一个位置传输到另一个位置。 这听起来像是一个非常简单的任务&#xff0c;但让我们通过声明这些文件可能包含机密信息并可以通过非安全的通信渠道进行传输这一事…

python中函数的定义实例_Python基础之函数的定义与使用实例

此文实例介绍了Python基础之函数的定义与使用。推荐给大伙学习一下&#xff0c;内容如下&#xff1a;Python 定义函数使用 def 关键字&#xff0c;一般格式如下&#xff1a;def 函数名(参数列表)&#xff1a;函数体让我们使用函数来输出"Hello World&#xff01;"&am…