带有Java 12和JHipster 6的更好,更快,更轻量的Java

最近,Java生态系统中发生了很多事情。 现在主要的Java版本每六个月发布一次,可能很难跟上。 我个人使用Spring Boot开发了大多数Java应用程序。 因此,在发布Spring Boot 2.1之前,我一直停留在Java 8上。

Spring Boot 2.1增加了对Java 11的支持,以及性能改进,新的指标实现,新的执行器端点和Spring Security的一流OIDC支持。

I was a web developer years before I became a Java developer. I’ve been developing Java for 20 years, so that’s saying a lot! If you’re a Java developer, chances are you’re baffled by the pace of change in the JavaScript world. If you think Java is hard to keep up with, try keeping up with JavaScript and its ecosystem! The good news is there’s an excellent way to keep up with both: just use JHipster.

JHipster 6使用Spring Boot 2.1作为其后端API,这意味着您可以使用Java 11+开发JHipster应用程序!

If you'd rather watch a video, I created a screencast of this blog post.

How Does JHipster Make Java and JavaScript Development Easier?

JHipster是我在这个星球上最喜欢的开源项目之一。 这是一个用于生成,开发和部署Spring Boot + Angular应用程序的开发平台。 此外,它支持React,Vue,并使用Netflix OSS,Elastic Stack和Docker创建微服务架构。

Java开发人员往往不喜欢JavaScript,但是许多人都喜欢TypeScript。 你猜怎么了? JHipster的所有UI框架实现均使用TypeScript!

JHipster通过为您配置所有内容并使用其基础框架的最新版本,使Java和JavaScript开发更加容易。 是否要升级您的应用程序以使用最新版本? 只需运行jhipster升级。

Install Java 11 or Java 12

Before you worry about installing JHipster, install Java 11. I recommend using SDKMAN! for this task. If you don’t have it installed, it’s just a command away:

curl -s "https://get.sdkman.io" | bash

安装后,您可以使用来查看所有可用的Java 11版本sdk列表java。

安装OpenJDK版本:

sdk install java 11.0.2-open

如果您想使用Amazon的Corretto,只需使用11.0.2-amzn为名字。 如果您想成为一个时髦的人,甚至可以使用Java 12!

sdk install java 12.0.0-open

小费:如果您使用,也可以使用Azul的Zulu12.0.0祖鲁为名字。

Install JHipster 6

A beta of JHipster 6 was released on May 3, 2019. You can install it using the command below.

npm install -g generator-jhipster@beta

ñOTE: The npm command is part of ñode.js. You’ll need to have Node 10.x to install JHipster and run useful commands.

Create a Spring Boot + Angular App that runs on Java 11+

开始使用JHipster的最基本方法是创建一个新目录,将其cd进入并运行吉普斯特。 系统将提示您回答有关您要创建的应用的许多问题。 问题范围从您的应用程序名称到您要使用的身份验证类型,再到SQL与NoSQL。

JHipster还具有一种域语言(称为JHipster域语言的JDL),可用于定义您的应用程序。

application {
  config {}
}

如果您使用上述代码创建应用,则将使用默认值:

  • baseName:吉普斯特应用类型:整体式databaseType:sql等等

ŤIP: You can see all the default values in JHipster’s JDL documentation.

要使用JHipster生成启用OAuth 2.0的应用,请创建一个app.jh文件放在新的项目目录中(例如,〜/ hipapp):

application {
  config {
    baseName hipapp
    authenticationType oauth2
  }
}

打开一个终端窗口,然后导航到与此文件相同的目录。 运行以下命令以使用Angular UI生成Spring Boot API。

警告:确保您不在主目录中! 您的项目将在与以下目录相同的目录中生成app。jh。

jhipster import-jdl app.jh

这将创建大量文件并使用以下命令安装依赖项npm安装。 运行此命令时,终端应类似于以下内容:

Results of import-jdl

如果您希望查看此命令在实际运行中的外观,则可以观看下面的记录。

由于您指定oauth2作为身份验证类型,将为Keycloak安装Docker Compose配置。

ķeycloak is an Apache-licensed open source identity and access management solution. In addition to creating a src/main/docker/keycloak.yml file for Docker Compose, JHipster generates a src/main/docker/config/realm-config directory with files in it that configure Keycloak to work with JHipster out-of-the-box.

Run Your JHipster App and Log in with Keycloak

Keycloak must be running for your JHipster app to start successfully. This is because Spring Security 5.1’s first-class OIDC support is leveraged in JHipster 6.

This OIDC support includes discovery, which means that Spring Security talks to a /.well-known/openid-configuration endpoint to configure itself. I completed the migration myself and deleted more code than I added!

使用Docker Compose启动Keycloak:

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

ŤIP: If you don’t have Docker Compose installed, see these instructions for how to install it.

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

./mvnw

应用启动并运行后,打开http://本地主机:8080在您喜欢的浏览器中,然后单击登入。 您将被重定向到Keycloak,您可以在其中输入管理员/管理员登录。

Welcome, Java Hipster

Pretty slick, eh? You just created a modern single page application (SPA) that uses the latest released version of Angular! Not only that, but it uses the most secure form of OAuth 2.0 - authorization code flow.

ñOTE: If you’re confused by how OAuth 2.0 and OpenID Connect (OIDC) work together, please see What the Heck is OAuth? In short, OIDC is a thin layer on top of OAuth 2.0 that adds identity.

Use Okta: We’re Always On!

Keycloak is an excellent project that works great for development and testing. However, if you use it in production, you’ll be responsible for maintaining it, updating it to the latest releases, and making sure it’s up 24/7. For these reasons, I recommend using Okta in production. After all, we’re always on! 😃

Create an OpenID Connect Web Application

To begin, sign up for a forever-free Okta developer account (or sign in to {yourOktaDomain} if you already have an account).

登录Okta后,注册您的客户端应用程序。

  • 在顶部菜单中,单击应用领域点击添加申请选择网页然后点击下一个输入很棒的OIDC为了名称(此值无关紧要,请随时进行更改)将登录重定向URI更改为http:// localhost:8080 / login / oauth2 / code / oidc请点击完成, 然后编辑并添加http://本地主机:8080作为注销重定向URI请点击保存

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

OIDC App Settings

创建一个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}"

小费:加* .env给你.gitignore文件,因此该文件不会最终出现在您的源代码管理系统中。

Create Groups and Add Them as Claims to the ID Token

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

创建一个ROLE_ADMIN和ROLE_USER组(用户数 > 团体 > 新增群组)和add users to them。 You can use the account you signed up with, or create a new user (用户数 > 加人)。 导航API > 授权服务器,和click on the the 默认服务器。 点击索偿 tab和添加声明。 命名团体,和include it in the ID Token。 Set the value type to 团体和set the filter to be a Regex of 。*。 请点击创建。

Add Claim

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

source okta.env
./mvnw

导航http://本地主机:8080并使用Okta凭据登录。

Authenticated by Okta

Spring Boot和Spring Security如何轻松切换OIDC提供程序不是很酷吗?

CRUD with JHipster

在这篇文章中,我几乎没有涉及JHipster所能提供的服务。 例如,您可以使用JDL为实体创建CRUD功能(带有测试!)。 例如,创建一个blog.jh文件与下面的代码。

entity Blog {
  name String required minlength(3),
  handle String required minlength(2)
}

entity BlogEntry {
  title String required,
  content TextBlob required,
  date Instant required
}

entity Tag {
  name String required minlength(2)
}

relationship ManyToOne {
  Blog{user(login)} to User,
  BlogEntry{blog(name)} to Blog
}

relationship ManyToMany {
  BlogEntry{tag(name)} to Tag{entry}
}

paginate BlogEntry, Tag with infinite-scroll

Then run jhipster import-jdl blog.jh in your project. The jdl-samples GitHub repository has many more examples.

Do More with JHipster

I’d like to thank Joe Grandja and Rob Winch from the Spring Security team. Without their help, JHipster’s migration to use Spring Security 5.1 would not have been possible. You guys rock!!

I did not create a GitHub repository for this post because all of the code was generated. You can find the source code for JHipster on GitHub.

Thanks JHipster and all its fabulous contributors. You all do a tremendous amount of work in your free time and it’s greatly appreciated.

还没准备好过渡到JHipster 6和Java 11+吗? 我写了一些使用JHipster 5和Java 8的教程。

I also wrote a free mini-book on JHipster for InfoQ.

如果您想了解有关Spring Security 5.1及其OIDC支持的更多信息,我们也提供了其中一些:

Follow us on @oktadev to stay up-to-date with Java and the leading JavaScript frameworks.

from: https://dev.to//oktadev/better-faster-lighter-java-with-java-12-and-jhipster-6-5a4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值