java hipster!_通过Java Hipster升级Spring Security OAuth和JUnit测试

java hipster!

“我喜欢编写身份验证和授权代码。” 〜从来没有Java开发人员。 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证。

使用单元测试和集成测试来验证代码质量是一种很好的方式来表明您对代码的关心。 最近,我在受欢迎的JHipster开源项目中做了很多工作,将其升级为使用最新版本的Spring Security。

Spring Security 5.1+将OAuth 2.0和OIDC添加为一流公民,您可以使用其优雅的DSL(又称酷方法链接,又称生成器模式)对其进行配置。 自从Rob Winch和工作人员首次启动它以来,我一直有使用它的动力。 与他们合作进行非常创新的项目很有趣。 Spring Security使OAuth很棒!

我在2017年秋天向JHipster添加了OAuth 2.0支持。这种经历对我产生了很大的影响。 我学到了很多有关Keycloak,Docker Compose以及如何在身份提供者(IdP)之间进行切换的知识。

我花了最后一个月升级JHipster以使用Spring Security 5.1(Spring Boot 2.1中的默认设置)。 在此过程中,我遇到了一些挫折,在Travis CI上摇了摇拳,当我想出解决方案时感到很高兴。 在此过程中,我也学到了很多东西。 今天,我将与您分享这些经验。

使用OAuth 2.0和OIDC注销

在JHipster中集成了对Keycloak和Okta的支持后不久,该项目收到了很多用户的抱怨,他们无法注销。 JHipster用户熟悉单击注销 (检查最新信息)并完全注销。 使用默认的Spring Security支持,用户将注销本地应用程序,而不是IdP。

我花了一年的时间,但终于在今年早些时候添加了全球SSO注销 。 Keycloak和Okta都要求您将GET请求发送到具有ID令牌和要重定向到的URL的端点。 因此,我创建了一个LogoutResource来返回这些值。

@RestController
public class LogoutResource {private final Logger log = LoggerFactory.getLogger(LogoutResource.class);private final UserInfoRestTemplateFactory templateFactory;private final String accessTokenUri;public LogoutResource(UserInfoRestTemplateFactory templateFactory,@Value("${security.oauth2.client.access-token-uri}") String accessTokenUri) {this.templateFactory = templateFactory;this.accessTokenUri = accessTokenUri;}/*** POST  /api/logout : logout the current user** @return the ResponseEntity with status 200 (OK) and a body with a global logout URL and ID token*/@PostMapping("/api/logout")public ResponseEntity<?> logout(HttpServletRequest request, Authentication authentication) {log.debug("REST request to logout User : {}", authentication);OAuth2RestTemplate oauth2RestTemplate = this.templateFactory.getUserInfoRestTemplate();String idToken = (String) oauth2RestTemplate.getAccessToken().getAdditionalInformation().get("id_token");String logoutUrl = accessTokenUri.replace("token", "logout");Map<String, String> logoutDetails = new HashMap<>();logoutDetails.put("logoutUrl", logoutUrl);logoutDetails.put("idToken", idToken);request.getSession().invalidate();return ResponseEntity.ok().body(logoutDetails);}
}

Angular客户端调用/api/logout端点并构造IdP注销URL。

this.authServerProvider.logout().subscribe(response => {const data = response.body;let logoutUrl = data.logoutUrl;// if Keycloak, uri has protocol/openid-connect/tokenif (logoutUrl.indexOf('/protocol') > -1) {logoutUrl = logoutUrl + '?redirect_uri=' + window.location.origin;} else {// OktalogoutUrl = logoutUrl + '?id_token_hint=' +data.idToken + '&post_logout_redirect_uri=' + window.location.origin;}window.location.href = logoutUrl;
});

测试LogoutResource非常简单。 大部分工作涉及模拟UserInfoRestTemplateFactory以便它返回ID令牌。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = JhipsterApp.class)
public class LogoutResourceIntTest {@Autowiredprivate MappingJackson2HttpMessageConverter jacksonMessageConverter;private final static String ID_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" +".eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsIm" +"p0aSI6ImQzNWRmMTRkLTA5ZjYtNDhmZi04YTkzLTdjNmYwMzM5MzE1OSIsImlhdCI6MTU0M" +"Tk3MTU4MywiZXhwIjoxNTQxOTc1MTgzfQ.QaQOarmV8xEUYV7yvWzX3cUE_4W1luMcWCwpr" +"oqqUrg";@Value("${security.oauth2.client.access-token-uri}")private String accessTokenUri;private MockMvc restLogoutMockMvc;@Beforepublic void before() {LogoutResource logoutResource = new LogoutResource(restTemplateFactory(), accessTokenUri);this.restLogoutMockMvc = MockMvcBuilders.standaloneSetup(logoutResource).setMessageConverters(jacksonMessageConverter).build();}@Testpublic void getLogoutInformation() throws Exception {String logoutUrl = accessTokenUri.replace("token", "logout");restLogoutMockMvc.perform(post("/api/logout")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(jsonPath("$.logoutUrl").value(logoutUrl)).andExpect(jsonPath("$.idToken").value(ID_TOKEN));}private UserInfoRestTemplateFactory restTemplateFactory() {UserInfoRestTemplateFactory factory = mock(UserInfoRestTemplateFactory.class);Map<String, Object> idToken = new HashMap<>();idToken.put("id_token", ID_TOKEN);DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("my-fun-token");token.setAdditionalInformation(idToken);when(factory.getUserInfoRestTemplate()).thenReturn(mock(OAuth2RestTemplate.class));when(factory.getUserInfoRestTemplate().getAccessToken()).thenReturn(token);return factory;}
}

我在1月下旬将全局注销支持合并到JHipster的master分支中,并在几周后开始升级Spring Security的OIDC支持。

升级Spring Security的OIDC支持
我从创建问题#9276开始,以跟踪我的目标,动机和已知问题。

在这一点上,如果您不熟悉Spring Security,您可能想知道:为什么升级到Spring Security的最新版本如此酷? 长话短说:它们已经弃用了注释,增加了功能,并使将OAuth 2.0和OIDC集成到您的应用程序中变得更加容易。 谢谢,Spring Security团队!

在Spring Boot 2.1+(即Spring Security 5.1+)中,不再建议使用@ EnableOAuth2Sso和@EnableResourceServer。 更改的原因可以在2019年1月25日发布的Josh Long的Bootiful Podcast中找到。这是Madhura Bhave的访谈,讨论从21:30开始。

除了将所有Java代码和YAML配置转换为使用最新的Spring Security比特外,我还决定默认情况下将每个JHipster应用程序配置为资源服务器。 这是JHipster的SecurityConfiguration.java.ejs模板中的逻辑:

@Override
public void configure(HttpSecurity http) throws Exception {// @formatter:offhttp...<%_ } else if (authenticationType === 'oauth2') { _%><%_ if (['monolith', 'gateway'].includes(applicationType)) { _%>.and().oauth2Login()<%_ } _%>.and().oauth2ResourceServer().jwt();<%_ } _%>// @formatter:on}
}

为了确保实现与OIDC兼容,我用进行观众验证的JwtDecoder bean覆盖了默认的JwtDecoder bean。

@Value("${spring.security.oauth2.client.provider.oidc.issuer-uri}")
private String issuerUri;@Bean
JwtDecoder jwtDecoder() {NimbusJwtDecoderJwkSupport jwtDecoder = (NimbusJwtDecoderJwkSupport)JwtDecoders.fromOidcIssuerLocation(issuerUri);OAuth2TokenValidator<Jwt> audienceValidator = new AudienceValidator();OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(issuerUri);OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer, audienceValidator);jwtDecoder.setJwtValidator(withAudience);return jwtDecoder;
}

在所有运行时代码正常工作之后,我开始进行重构测试。 测试是重构成功的最可靠指标,尤其是对于像JHipster这样具有26,000个组合的项目而言!

在此过程中,我遇到了许多挑战。 由于我学到了很多解决这些挑战的知识,所以我认为对它们进行说明以及如何解决它们很有趣。

如何模拟具有ID令牌的AuthenticatedPrincipal

我遇到的第一个挑战是更新的LogoutResource 。 下面是将其重构为使用Spring Security的ClientRegistrationRepository之后的代码。

@RestController
public class LogoutResource {private ClientRegistration registration;public LogoutResource(ClientRegistrationRepository registrations) {this.registration = registrations.findByRegistrationId("oidc");}/*** {@code POST  /api/logout} : logout the current user.** @param request the {@link HttpServletRequest}.* @param idToken the ID token.* @return the {@link ResponseEntity} with status {@code 200 (OK)} and a body with a global logout URL and ID token.*/@PostMapping("/api/logout")public ResponseEntity<?> logout(HttpServletRequest request,@AuthenticationPrincipal(expression = "idToken") OidcIdToken idToken) {String logoutUrl = this.registration.getProviderDetails().getConfigurationMetadata().get("end_session_endpoint").toString();Map<String, String> logoutDetails = new HashMap<>();logoutDetails.put("logoutUrl", logoutUrl);logoutDetails.put("idToken", idToken.getTokenValue());request.getSession().invalidate();return ResponseEntity.ok().body(logoutDetails);}
}

我试图在LogoutResourceIT.java模拟OAuth2AuthenticationToken ,以为这将导致AuthenticationPrincipal的填充。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = JhipsterApp.class)
public class LogoutResourceIT {@Autowiredprivate ClientRegistrationRepository registrations;@Autowiredprivate MappingJackson2HttpMessageConverter jacksonMessageConverter;private final static String ID_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" +".eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsIm" +"p0aSI6ImQzNWRmMTRkLTA5ZjYtNDhmZi04YTkzLTdjNmYwMzM5MzE1OSIsImlhdCI6MTU0M" +"Tk3MTU4MywiZXhwIjoxNTQxOTc1MTgzfQ.QaQOarmV8xEUYV7yvWzX3cUE_4W1luMcWCwpr" +"oqqUrg";private MockMvc restLogoutMockMvc;@Beforepublic void before() {LogoutResource logoutResource = new LogoutResource(registrations);this.restLogoutMockMvc = MockMvcBuilders.standaloneSetup(logoutResource).setMessageConverters(jacksonMessageConverter).build();}@Testpublic void getLogoutInformation() throws Exception {Map<String, Object> claims = new HashMap<>();claims.put("groups", "ROLE_USER");claims.put("sub", 123);OidcIdToken idToken = new OidcIdToken(ID_TOKEN, Instant.now(),Instant.now().plusSeconds(60), claims);String logoutUrl = this.registrations.findByRegistrationId("oidc").getProviderDetails().getConfigurationMetadata().get("end_session_endpoint").toString();restLogoutMockMvc.perform(post("/api/logout").with(authentication(createMockOAuth2AuthenticationToken(idToken)))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(jsonPath("$.logoutUrl").value(logoutUrl));}private OAuth2AuthenticationToken createMockOAuth2AuthenticationToken(OidcIdToken idToken) {Collection<GrantedAuthority> authorities = new ArrayList<>();authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));OidcUser user = new DefaultOidcUser(authorities, idToken);return new OAuth2AuthenticationToken(user, authorities, "oidc");}
}

但是,这导致以下错误:

Caused by: java.lang.IllegalArgumentException: tokenValue cannot be emptyat org.springframework.util.Assert.hasText(Assert.java:284)at org.springframework.security.oauth2.core.AbstractOAuth2Token.<init>(AbstractOAuth2Token.java:55)at org.springframework.security.oauth2.core.oidc.OidcIdToken.<init>(OidcIdToken.java:53)at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172)

我将这个问题发布到Stack Overflow上,并且还向Spring Security团队发送了电子邮件。 Joe Grandja对此问题做出了回应。

AuthenticationPrincipalArgumentResolver未在您的测试中注册。
启用“完整” spring-web-mvc时,它将自动注册,例如@EnableWebMvc

但是,在您的@Before ,您具有:

MockMvcBuilders.standaloneSetup() –这不会初始化完整的web-mvc基础结构–只是一个子集。

尝试以下方法:
MockMvcBuilders.webAppContextSetup(this.context) –这将注册AuthenticationPrincipalArgumentResolver并且您的测试应解析OidcIdToken

乔是正确的。 我将测试更改为以下内容,并通过了测试。 ✅

@RunWith(SpringRunner.class)
@SpringBootTest(classes = JhipsterApp.class)
public class LogoutResourceIT {@Autowiredprivate ClientRegistrationRepository registrations;@Autowiredprivate WebApplicationContext context;private final static String ID_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" +".eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsIm" +"p0aSI6ImQzNWRmMTRkLTA5ZjYtNDhmZi04YTkzLTdjNmYwMzM5MzE1OSIsImlhdCI6MTU0M" +"Tk3MTU4MywiZXhwIjoxNTQxOTc1MTgzfQ.QaQOarmV8xEUYV7yvWzX3cUE_4W1luMcWCwpr" +"oqqUrg";private MockMvc restLogoutMockMvc;@Beforepublic void before() throws Exception {Map<String, Object> claims = new HashMap<>();claims.put("groups", "ROLE_USER");claims.put("sub", 123);OidcIdToken idToken = new OidcIdToken(ID_TOKEN, Instant.now(),Instant.now().plusSeconds(60), claims);SecurityContextHolder.getContext().setAuthentication(authenticationToken(idToken));SecurityContextHolderAwareRequestFilter authInjector = new SecurityContextHolderAwareRequestFilter();authInjector.afterPropertiesSet();this.restLogoutMockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();}@Testpublic void getLogoutInformation() throws Exception {String logoutUrl = this.registrations.findByRegistrationId("oidc").getProviderDetails().getConfigurationMetadata().get("end_session_endpoint").toString();restLogoutMockMvc.perform(post("/api/logout")).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(jsonPath("$.logoutUrl").value(logoutUrl)).andExpect(jsonPath("$.idToken").value(ID_TOKEN));}private OAuth2AuthenticationToken authenticationToken(OidcIdToken idToken) {Collection<GrantedAuthority> authorities = new ArrayList<>();authorities.add(new SimpleGrantedAuthority(AuthoritiesConstants.USER));OidcUser user = new DefaultOidcUser(authorities, idToken);return new OAuth2AuthenticationToken(user, authorities, "oidc");}
}

正确测试注销功能是一个重要的里程碑。 我继续升级JHipster的微服务架构。

如何使用Zuul将OAuth 2.0访问令牌传递给下游微服务

JHipster使用Netflix Zuul代理从网关到下游微服务的请求。 我创建了一个AuthorizationHeaderFilter来处理访问令牌传播。

public class AuthorizationHeaderFilter extends ZuulFilter {private final AuthorizationHeaderUtil headerUtil;public AuthorizationHeaderFilter(AuthorizationHeaderUtil headerUtil) {this.headerUtil = headerUtil;}@Overridepublic String filterType() {return PRE_TYPE;}@Overridepublic int filterOrder() {return Ordered.LOWEST_PRECEDENCE;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() {RequestContext ctx = RequestContext.getCurrentContext();Optional<String> authorizationHeader = headerUtil.getAuthorizationHeader();authorizationHeader.ifPresent(s -> ctx.addZuulRequestHeader(TokenRelayRequestInterceptor.AUTHORIZATION, s));return null;}
}

但是,添加它不会导致成功访问令牌传播。 在Jon Ruddell的帮助下 ,我发现这是因为JHipster有一个LazyInitBeanFactoryPostProcessor导致所有bean都被延迟加载。 该ZuulFilterInitializer中包含ZuulFilterInitializer 。 将ZuulFilterInitializer为热切加载的bean,可以使一切正常工作。

至此,我一切正常,因此创建了一个pull请求来升级JHipster的模板 。

我知道我签入的内容需要运行Keycloak才能通过集成测试。 这是由于OIDC发现以及如何从.well-known/openid-configuration查找端点。

在Spring Boot集成测试中如何处理OIDC发现

我不太担心Keycloak是否需要运行才能通过集成测试。 然后,我们的某些Azure和Travis构建开始失败。 JHipster开发人员指出,当Keycloak不运行时,他们会看到类似以下的错误。

Factory method 'clientRegistrationRepository' threw exception; nested exception is
java.lang.IllegalArgumentException: Unable to resolve the OpenID Configuration
with the provided Issuer of "http://localhost:9080/auth/realms/jhipster"

我通过Spring Security的OAuth和OIDC测试进行了一些摸索,并提出了一个解决方案 。 该修复程序涉及添加一个TestSecurityConfiguration类,该类将覆盖默认的Spring Security设置并模拟Bean,从而不会发生OIDC发现。

@TestConfiguration
public class TestSecurityConfiguration {private final ClientRegistration clientRegistration;public TestSecurityConfiguration() {this.clientRegistration = clientRegistration().build();}@BeanClientRegistrationRepository clientRegistrationRepository() {return new InMemoryClientRegistrationRepository(clientRegistration);}private ClientRegistration.Builder clientRegistration() {Map<String, Object> metadata = new HashMap<>();metadata.put("end_session_endpoint", "https://jhipster.org/logout");return ClientRegistration.withRegistrationId("oidc").redirectUriTemplate("{baseUrl}/{action}/oauth2/code/{registrationId}").clientAuthenticationMethod(ClientAuthenticationMethod.BASIC).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).scope("read:user").authorizationUri("https://jhipster.org/login/oauth/authorize").tokenUri("https://jhipster.org/login/oauth/access_token").jwkSetUri("https://jhipster.org/oauth/jwk").userInfoUri("https://api.jhipster.org/user").providerConfigurationMetadata(metadata).userNameAttributeName("id").clientName("Client Name").clientId("client-id").clientSecret("client-secret");}@BeanJwtDecoder jwtDecoder() {return mock(JwtDecoder.class);}@Beanpublic OAuth2AuthorizedClientService authorizedClientService(ClientRegistrationRepository clientRegistrationRepository) {return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository);}@Beanpublic OAuth2AuthorizedClientRepository authorizedClientRepository(OAuth2AuthorizedClientService authorizedClientService) {return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(authorizedClientService);}
}

然后,在使用@SpringBootTest类中,我将其配置为配置源。

@SpringBootTest(classes = {MicroApp.class, TestSecurityConfiguration.class})

在使用OAuth 2.0保护的JHipster微服务上运行端到端测试

最终问题很快就浮出水面了。 jhipster-daily-build (在Azure DevOps上运行)在尝试测试微服务时失败。

Caused by: java.lang.IllegalArgumentException: Unable to resolve the OpenID Configurationwith the provided Issuer of "http://localhost:9080/auth/realms/jhipster"

我们不包括用于微服务的Keycloak Docker Compose文件,因为我们不希望它们独立运行。 他们需要网关才能访问它们,因此其OAuth 2.0设置应与您的网关匹配,并且网关项目中包含Keycloak文件。

在Azure上运行的端到端测试,其中1)启动微服务,2)达到其运行状况终结点以确保其成功启动。 为了解决此问题, Pascal Grimaud 禁用了启动/测试微服务 。 他还创建了一个新问题来改进流程,因此可以使用JHipster的JDL生成完整的微服务堆栈。

升级到Spring Security 5.1及其一流的OIDC支持

我希望这些挑战和修复方法列表对您有所帮助。 如果您使用不推荐使用的@EnableOAuth2Sso@EnableResourceServer ,我建议您尝试升级到Spring Security 5.1。 我用来跟踪升级的问题包含显示所有必需的代码更改的链接。

  • 整体所需的代码更改
  • 微服务架构所需的代码更改

使用JHipster 6生成带有OIDC进行身份验证的Spring Boot + React应用

JHipster 6使用最新和最好的Spring Boot和Spring Security版本。 它的前端支持Angular和React。 它也支持Vue ,它不是主要生成器的一部分。

如果使用JHipster 6生成应用程序,则本文中提到的所有测试功能都将包含在您的应用程序中。 你是怎样做的? 我很高兴你问!
首先安装JHipster 6 Beta:

npm install -g generator-jhipster@beta

npm命令是Node.js的一部分。 您将需要Node 10.x来安装JHipster并运行有用的命令。

JHipster 6支持Java 8、11和12(感谢Spring Boot 2.1)。 我建议使用SDKMAN管理Java SDK ! 例如,您可以安装Java 12并将其设置为默认值。

sdk install java 12.0.0-open
sdk default java 12.0.0-open

您可以创建一个使用React和OIDC的JHipster应用,只需几个命令:

mkdir app && cd appecho "application { config { baseName reactoidc, \authenticationType oauth2, clientFramework react } }" >> app.jhjhipster import-jdl app.jh

下面是显示这些命令结果的终端记录。

必须已配置的OIDC提供程序正在运行,JHipster生成的Spring Boot应用程序才能成功启动。 您可以使用Docker Compose启动Keycloak:

docker-compose -f src/main/docker/keycloak.yml up -d

然后使用Maven启动您的应用程序:

./mvnw

启动完成后,打开http://localhost:8080 ,然后单击登录 。 您将被重定向到Keycloak,您可以在其中输入admin/admin登录。

为什么用Okta代替Keycloak?

Keycloak的效果很好,但这是Okta开发人员博客上的文章,所以让我向您展示如何使用Okta! 为什么要使用Okta? 这是一个很好的问题。
Okta是永远在线的身份提供者,为开发人员提供身份验证和授权服务。 它还允许您管理用户。 我喜欢将其称为“用户作为软件服务”,但UASS并不是一个很好的缩写。 用户管理作为软件服务(UMASS)可以轻松解决。 无论如何,这是一项很棒的服务,您应该尝试一下。

注册您的安全Spring Boot应用程序

首先,注册一个免费的Okta开发人员帐户 (如果已经有一个帐户,则登录)。

登录Okta后,注册您的Spring Boot应用程序。

  • 在顶部菜单中,单击“ 应用程序”
  • 点击添加应用
  • 选择网站 ,然后单击下一步。
  • 输入名字
  • 将登录重定向URI更改为http://localhost:8080/login/oauth2/code/oidc
  • 点击完成 ,然后点击编辑 ,然后添加http://localhost:8080作为注销重定向URI。
  • 点击保存

完成后,您的设置应类似于以下屏幕截图。

在项目的根目录中创建okta.env文件,并将{..}值替换为Okta应用程序中的值:

export SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_OIDC_ISSUER_URI=https://{yourOktaDomain}/oauth2/default
export SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_CLIENT_ID={clientId}
export SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_CLIENT_SECRET={clientSecret}

在您的.gitignore文件中添加*.env ,这样该文件就不会在GitHub上结束。

创建组并将其作为声明添加到ID令牌

默认情况下,JHipster配置为与两种类型的用户一起使用:管理员和用户。 Keycloak会自动为用户和组配置,但是您需要为Okta组织进行一次一次性配置。

创建一个ROLE_ADMINROLE_USER组(“ 用户” >“ 组” >“ 添加组” )并将用户添加到其中。 您可以使用注册时使用的帐户,也可以创建一个新用户(“ 用户” >“ 添加人” )。 导航到API > 授权服务器 ,然后单击default服务器。 点击索赔标签,然后添加索赔 。 将其命名为groups ,并将其包含在ID令牌中。 将值类型设置为Groups并将过滤器设置为.*的正则表达式。 点击创建

使用以下命令启动您的应用程序:

source okta.env
./mvnw

导航到http://localhost:8080并使用Okta凭据登录。

漂亮的臀部,你不觉得吗? 🤓

使用JHipster进行更好的Java测试

JHipster为您生成了一个具有开箱即用的良好测试覆盖范围的应用程序。 使用自动为您配置的SonarCloud分析代码覆盖率。 运行以下命令以在Docker容器中启动Sonar。

docker-compose -f src/main/docker/sonar.yml up -d

然后运行以下Maven命令:

./mvnw -Pprod clean test sonar:sonar -Dsonar.host.url=http://localhost:9001

该过程完成后,导航至http://localhost:9001/projects ,您将看到项目的报告。

代码覆盖率比本报告中显示的要高得多。 我们最近更改了许多测试以在集成测试阶段运行,并且还没有弄清楚如何将此数据报告给Sonar。

有关此功能的更多信息,请参见JHipster的代码质量文档 。

对JHipster中的JUnit 5的支持也在进行中 。

了解有关Spring Security,Spring Boot和JHipster的更多信息

我希望您喜欢我有关升级JHipster以使用Spring Security 5.1及其出色的OAuth 2.0 + OIDC支持的故事。 我真的很喜欢Spring Security团队所做的工作,以简化其配置并使OIDC发现(以及其他功能)正常工作。

由于JHipster生成了所有代码,因此我没有为该示例创建GitHub存储库,因此不需要修改任何内容。

如果您想了解有关JHipster 6的更多信息,请参阅Java 12和JHipster 6更好,更快,更轻便的Java 。 如果您对JHipster的CRUD生成功能和PWA支持感兴趣,我鼓励您阅读我的博客文章, 了解如何使用React,Spring Boot和JHipster构建照相馆PWA 。

我们还发布了许多有关测试和Spring Security 5.1的文章:

  • 使用JUnit 5测试您的Spring Boot应用程序
  • 使用WireMock,Jest,Protractor和Travis CI测试Spring Boot API和Angular组件的Hitchhiker指南
  • 带有Spring Security的OAuth 2.0快速指南
  • 将您的Spring Boot应用程序迁移到最新和最新的Spring Security和OAuth 2.0

需要更多技术提示吗? 在社交网络{ Twitter , LinkedIn , Facebook , YouTube }上关注我们,以便在我们发布新内容时得到通知。
是否有与Okta无关的问题? 请在我们的开发人员论坛上提问。

“通过Java Hipster升级Spring Security OAuth和JUnit测试”最初于2019年4月15日发布在Okta开发人员博客上。

“我喜欢编写身份验证和授权代码。” 〜从来没有Java开发人员。 厌倦了一次又一次地建立相同的登录屏幕? 尝试使用Okta API进行托管身份验证,授权和多因素身份验证。

翻译自: https://www.javacodegeeks.com/2019/05/spring-security-oauth-through-java-hipster.html

java hipster!

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

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

相关文章

在 Mac 上显示或隐藏文件扩展名

文章目录对于一个文件对于所有文件通过命令来设置更改扩展名显示警告对于一个文件 在 Mac 上&#xff0c;选择文件&#xff0c;然后选取“文件”>“显示简介”&#xff0c;或按下 Command-I。 点按“名称与扩展名”旁边的三角形来展开下面的部分。 若要显示或隐藏文件扩展…

dism++封装系统使用教程_客栈管理系统“订单来了”客房订单盒子使用教程

原标题&#xff1a;客栈管理系统“订单来了”客房订单盒子使用教程「订单来了」是非标住宿行业领先的移动互联云PMS&#xff0c;是以PMS为基础&#xff0c;聚合全渠道的营销管理生态平台&#xff0c;为客栈/民宿提供信息化一体解决方案。已累计服务国内外400城市、30000商家&am…

文本替换sed+字段处理cut,join+awk重新编排字段

【1】&#xff53;&#xff45;&#xff44;工具(Stream Editor)——流编辑器 sed 本身也是一个管线&#xff08;管道&#xff09;命令&#xff0c;可以分析 standard input 的啦&#xff01; 而且 sed 还可以将数据进行取代、删除、新增、截取特定行等等的功能呢&#xff01;…

netsuite 数据集成_Java中带有NetSuite数据实体的对象关系映射(ORM)

netsuite 数据集成对象关系映射&#xff08;ORM&#xff09;技术使使用关系数据源更加容易&#xff0c;并且可以将逻辑业务模型与物理存储模型联系在一起。 遵循本教程&#xff0c;将NetSuite数据的连接集成到基于Java的ORM框架Hibernate中。 您可以使用Hibernate将面向对象的…

python内置装饰器property_python之内置装饰器(property/staticmethod/classmethod)

python内置了property、staticmethod、classmethod三个装饰器&#xff0c;有时候我们也会用到&#xff0c;这里简单说明下1、property作用&#xff1a;顾名思义把函数装饰成属性一般我们调用类方法成员&#xff0c;都是如下写法&#xff1a;class propertyTest():def __init__(…

关于bochs用X11启动的说明

关于网络上有很多关于 bochs启动时 连接到X Windows 报错&#xff1a; cannot connect to X display . 昨天搞了半天晚上 今天早上 直到现在 才搞懂。 主要的原因 是我使用了终端 比如 xshell or putty 去连接 centos &#xff0c;而以这种方式的连接 是只能够 传递字符&a…

在MyEclipse中访问MySql

Window 选项卡 选择 Open PerSpective 选择 MyEclipse Database Explorer&#xff0c;打开数据库视图 在 DB Browser 中单击右键&#xff0c;选择 New … 在 Database Driver 界面配置 a. Driver template: 连接的数据库模板 b. Driver name: 自定义的名字 c. Connection URL&a…

自动化测试框架 2019_2019年用于自动化的5个最佳Java测试框架

自动化测试框架 2019几十年来&#xff0c;Java一直是开发应用程序服务器端层的首选编程语言。 尽管JUnit一直在与开发人员一起帮助他们进行自动化的单元测试&#xff0c;但随着时间的推移和测试的发展&#xff0c;当自动化测试不断增长时&#xff0c;已经开发了许多基于Java的开…

X86汇编语言中的registers相关

0、写在前面 本文中总结于王爽老师的汇编语言&#xff0c;建议有兴趣的都买一本&#xff0c;以支持王爽老师的辛勤付出。再者&#xff0c;这本书写的确实很nice。 8086CPU共有14个registers&#xff1a;AX&#xff0c; BX&#xff0c; CX&#xff0c; DX&#xff0c; SI&#…

代码流程图生成器_【进阶Python】第五讲:迭代器与生成器

点击蓝字关注我迭代是Python中常用且非常强大的一个功能&#xff0c;它可以用于访问集合、列表、字符串、字典等数据结构的元素。我们经常使用循环和条件语句&#xff0c;我们也清楚哪些是可以迭代访问&#xff0c;但是具体它们之间有什么有什么异同之处&#xff1f;有哪些特点…

生成最简单的验证码图片的Java代码

后端代码&#xff1a; package priv.lwx.servlet.sl.web; /*** description** author liaowenxiong* date 2022/3/25 09:56*/import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.…

cognito_将Amazon Cognito与单页面应用程序(Vue.js)集成

cognito在本文中&#xff0c;我们将研究使用OAuth协议通过Amazon Cognito对单页应用程序&#xff08;使用Vue.js构建&#xff09;进行身份验证。 在上一篇文章中&#xff0c;我们将服务器端应用程序与Amazon Cognito集成在一起。 搭建单页应用程序 我们将使用vue-cli创建一个空…

段描述符表(GDT+LDT)的有感

【0】写在前面 要知道&#xff0c;在汇编中&#xff0c;代码的装入顺序决定了在内存中的地址位置。所有的代码或者数据都在硬盘上&#xff0c;当调试或者启动的时候&#xff0c;加载到内存&#xff1b;当需要对数据进行处理的时候&#xff0c;我们通过将数据从内存载入到regis…

地理信息系统概论_地理信息系统概论黄杏元第3版配套练习题库——才聪学习网...

黄杏元《地理信息系统概论》(第3版)配套题库【考研真题精选&#xff0b;章节题库】一、选择题1完整的GIS主要由四个部分构成&#xff0c;即计算机硬件系统、计算机软件系统、(  )和系统管理操作人员。[杭州师范大学2018年研]A&#xff0e;网络B&#xff0e;用户C&#xff0e;…

PO/BO/VO/DTO/POJO/DAO/DO

文章目录DO&#xff08;Domain Object&#xff09;DO&#xff08;Data Object&#xff09;POVOBODTOPOJODAOJavaBeanEJBEntity应用程序的分层设计MVC业务分层阿里开发手册关于POJO类的命名规范总结参考文章DO&#xff08;Domain Object&#xff09; Domain Object&#xff0c;…

openshift_OpenShift Origin中的Kubernetes Spark运算符(第1部分)

openshift本系列有关Radanalytics.io的Kubernetes Spark运算符 OpenShift起源 。 它是一个开源的运营商来管理 Apache Spark集群和应用程序。 为了在OpenShift Origin上部署操作员&#xff0c;第一次需要为其克隆GitHub存储库&#xff1a; git clone https://github.com/rad…

小程序二级页面tabbar_小程序页面推广踩坑记

前几天&#xff0c;团队里有个小伙伴遇到一个问题&#xff0c;卡壳了大半天。我觉得这个例子值得拿出来分享给大家。希望大家以后遇到类似的问题能顺利绕过这种坑。业务场景很常见&#xff0c;就是为了配合小程序中特定页面的推广&#xff0c;区分推广渠道和人员&#xff0c;逐…

转移指令jmp和跳转指令call

【-1】写在前面 以下内容文字描述来自于 王爽老师的《汇编语言》教材&#xff0c;建议大家都买一本&#xff0c;哈哈。不是我打广告&#xff0c;确实人家写的好&#xff0c;应该支持。我只是附上了自己的图片和理解而已。 【0】先上干货 只修改ip&#xff0c;称为段内转移&a…

jmeter负载测试测试_使用Apache JMeter负载测试Web应用程序

jmeter负载测试测试Apache JMeter是用于模拟Web应用程序上的用户负载以测试性能的出色工具。 您可以通过指定用户数量和请求间隔来轻松地构建测试计划&#xff0c;然后JMeter将为每个用户生成一个线程并访问您的Web应用程序。 在测试结束时&#xff0c;您将获得一份性能摘要报告…

Windows下安装Nutch

Nutch安装 一、需求部份 a) Nutch是Java开发的所以需要下载Java JDK 下载地址http://java.sun.com/javase/downloads/index.jsp b) Nutch的演示搜索页面是Jsp的需要Tomcat做服务器 下载地址&#xff1a;http://jakarta.apache.org/tomcat/ c) Nutch的脚本都是用Linux的Shell写的…