天天看點

使用Java EE和OIDC建構Java REST API

“我喜歡編寫身份驗證和授權代碼。” 〜從來沒有Java開發人員。

厭倦了一次又一次地建立相同的登入螢幕? 嘗試使用Okta API進行托管身份驗證,授權和多因素身份驗證。

Java EE允許您使用JAX-RS和JPA快速輕松地建構Java REST API。 Java EE是保護傘标準規範,它描述了許多Java技術,包括EJB,JPA,JAX-RS和許多其他技術。 它最初旨在允許Java應用程式伺服器之間的可移植性,并在2000年代初期蓬勃發展。 那時,應用伺服器非常流行,并且由許多知名公司(例如IBM,BEA和Sun)提供。 JBoss是一家新興公司,它破壞了現狀,并表明可以将Java EE應用程式伺服器開發為一個開源項目,并免費提供它。 JBoss在2006年被RedHat收購。

在2000年代初期,Java開發人員使用servlet和EJB來開發其伺服器應用程式。 Hibernate和Spring分别于2002年和2004年問世。 兩種技術都對各地的Java開發人員産生了巨大的影響,這表明他們有可能在沒有EJB的情況下編寫分布式,健壯的應用程式。 Hibernate的POJO模型最終被用作JPA标準,并且對EJB的影響也很大。

快進到2018年,Java EE肯定不像以前那樣! 現在,它主要是POJO和注釋,并且使用起來更簡單。

為什麼要使用Java EE而不是Spring Boot建構Java REST API?

Spring Boot是Java生态系統中我最喜歡的技術之一。 它極大地減少了Spring應用程式中必需的配置,并使得僅用幾行代碼即可生成REST API。 但是,最近有一些不使用Spring Boot的開發人員提出了很多API安全性問題。 其中一些甚至沒有使用Spring!

基于這個原因,我認為建構一個Java REST API(使用Java EE)很有趣,該API與我過去開發的Spring Boot REST API相同。 即,我的Bootiful Angular和Bootiful React文章中的“啤酒” API。

使用Java EE建構Java REST API

首先,我在Twitter上詢問了我的網絡,是否存在諸如start.spring.io之類的Java EE快速入門。 我收到了一些建議,并開始進行一些研究。 David Blevins建議我看一下tomee-jaxrs-starter-project ,是以我從那裡開始。 我還研究了Roberto Cortez推薦的TomEE Maven原型 。

我喜歡jaxrs-starter項目,因為它展示了如何使用JAX-RS建立REST API。 TomEE Maven原型也很有用,特别是因為它展示了如何使用JPA,H2和JSF。 我将兩者結合起來,建立了自己的最小啟動器,可用于在TomEE上實作安全的Java EE API。 您不必在這些示例中使用TomEE,但我尚未在其他實作上對其進行測試。

如果您在其他應用伺服器上使用了這些示例,請告訴我,我将更新此部落格文章。

在這些示例中,我将使用Java 8和Java EE 7.0以及TomEE 7.1.0。 TomEE 7.x是EE 7相容版本; 有一個TomEE 8.x分支用于EE8相容性工作,但尚無發行版本。 我希望您也安裝了Apache Maven 。

首先,将我們的Java EE REST API存儲庫克隆到您的硬碟驅動器,然後運作它:

git clone https://github.com/oktadeveloper/okta-java-ee-rest-api-example.git javaee-rest-api
cd javaee-rest-api
mvn package tomee:run
           

導航到http:// localhost:8080并添加新啤酒。

使用Java EE和OIDC建構Java REST API

單擊添加 ,您應該看到成功消息。

使用Java EE和OIDC建構Java REST API

單擊檢視存在的啤酒以檢視啤酒的完整清單。

使用Java EE和OIDC建構Java REST API

您還可以在

http://localhost:8080/good-beers

檢視系統中的優質啤酒清單。 以下是使用HTTPie時的輸出。

$ http :8080/good-beers
HTTP/1.1 200
Content-Type: application/json
Date: Wed, 29 Aug 2018 21:58:23 GMT
Server: Apache TomEE
Transfer-Encoding: chunked
           
[
    {
        "id": 101,
        "name": "Kentucky Brunch Brand Stout"
    },
    {
        "id": 102,
        "name": "Marshmallow Handjee"
    },
    {
        "id": 103,
        "name": "Barrel-Aged Abraxas"
    },
    {
        "id": 104,
        "name": "Heady Topper"
    },
    {
        "id": 108,
        "name": "White Rascal"
    }
]
           

使用Java EE建構REST API

我向您展示了該應用程式可以做什麼,但是我還沒有談論它是如何建構的。 它有一些XML配置檔案,但我将跳過其中的大多數。 目錄結構如下所示:

$ tree .
.
├── LICENSE
├── README.md
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── okta
    │   │           └── developer
    │   │               ├── Beer.java
    │   │               ├── BeerBean.java
    │   │               ├── BeerResource.java
    │   │               ├── BeerService.java
    │   │               └── StartupBean.java
    │   ├── resources
    │   │   └── META-INF
    │   │       └── persistence.xml
    │   └── webapp
    │       ├── WEB-INF
    │       │   ├── beans.xml
    │       │   └── faces-config.xml
    │       ├── beer.xhtml
    │       ├── index.jsp
    │       └── result.xhtml
    └── test
        └── resources
            └── arquillian.xml

12 directories, 16 files
           

最重要的XML檔案是

pom.xml

,它定義了依賴關系,并允許您運作TomEE Maven插件。 它非常簡短,可愛,隻有一個依賴項和一個插件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.okta.developer</groupId>
    <artifactId>java-ee-rest-api</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Java EE Webapp with JAX-RS API</name>
    <url>http://developer.okta.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <javaee-api.version>7.0</javaee-api.version>
        <tomee.version>7.1.0</tomee.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee-api.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomee.maven</groupId>
                <artifactId>tomee-maven-plugin</artifactId>
                <version>${tomee.version}</version>
                <configuration>
                    <context>ROOT</context>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
           

主要實體是

Beer.java

package com.okta.developer;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Beer {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;

    public Beer() {}

    public Beer(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String beerName) {
        this.name = beerName;
    }

    @Override
    public String toString() {
        return "Beer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
           

資料庫(aka,資料源)在

src/main/resources/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="beer-pu" transaction-type="JTA">
        <jta-data-source>beerDatabase</jta-data-source>
        <class>com.okta.developer.Beer</class>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
        </properties>
    </persistence-unit>
</persistence>
           

BeerService.java

類使用JPA的

EntityManager

處理該實體的讀取并将其儲存到資料庫中。

package com.okta.developer;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import java.util.List;

@Stateless
public class BeerService {

    @PersistenceContext(unitName = "beer-pu")
    private EntityManager entityManager;

    public void addBeer(Beer beer) {
        entityManager.persist(beer);
    }

    public List<Beer> getAllBeers() {
        CriteriaQuery<Beer> cq = entityManager.getCriteriaBuilder().createQuery(Beer.class);
        cq.select(cq.from(Beer.class));
        return entityManager.createQuery(cq).getResultList();
    }

    public void clear() {
        Query removeAll = entityManager.createQuery("delete from Beer");
        removeAll.executeUpdate();
    }
}
           

有一個

StartupBean.java

,用于在啟動時填充資料庫,并在關閉時清除資料庫。

package com.okta.developer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import java.util.stream.Stream;

@Singleton
@Startup
public class StartupBean {
    private final BeerService beerService;

    @Inject
    public StartupBean(BeerService beerService) {
        this.beerService = beerService;
    }

    @PostConstruct
    private void startup() {
        // Top beers from https://www.beeradvocate.com/lists/top/
        Stream.of("Kentucky Brunch Brand Stout", "Marshmallow Handjee", 
                "Barrel-Aged Abraxas", "Heady Topper",
                "Budweiser", "Coors Light", "PBR").forEach(name ->
                beerService.addBeer(new Beer(name))
        );
        beerService.getAllBeers().forEach(System.out::println);
    }

    @PreDestroy
    private void shutdown() {
        beerService.clear();
    }
}
           

這三個類構成了應用程式的基礎,此外還有一個

BeerResource.java

類,它使用JAX-RS公開

/good-beers

端點。

package com.okta.developer;

import javax.ejb.Lock;
import javax.ejb.Singleton;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.List;
import java.util.stream.Collectors;

import static javax.ejb.LockType.READ;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

@Lock(READ)
@Singleton
@Path("/good-beers")
public class BeerResource {
    private final BeerService beerService;

    @Inject
    public BeerResource(BeerService beerService) {
        this.beerService = beerService;
    }

    @GET
    @Produces({APPLICATION_JSON})
    public List<Beer> getGoodBeers() {
        return beerService.getAllBeers().stream()
                .filter(this::isGreat)
                .collect(Collectors.toList());
    }

    private boolean isGreat(Beer beer) {
        return !beer.getName().equals("Budweiser") &&
                !beer.getName().equals("Coors Light") &&
                !beer.getName().equals("PBR");
    }
}
           

最後,有一個

BeerBean.java

類用作JSF的托管bean。

package com.okta.developer;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Named
@RequestScoped
public class BeerBean {

    @Inject
    private BeerService beerService;
    private List<Beer> beersAvailable;
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Beer> getBeersAvailable() {
        return beersAvailable;
    }

    public void setBeersAvailable(List<Beer> beersAvailable) {
        this.beersAvailable = beersAvailable;
    }

    public String fetchBeers() {
        beersAvailable = beerService.getAllBeers();
        return "success";
    }

    public String add() {
        Beer beer = new Beer();
        beer.setName(name);
        beerService.addBeer(beer);
        return "success";
    }
}
           

您現在擁有了使用Java EE建構的REST API! 但是,這并不安全。 在以下各節中,我将向您展示如何使用Okta的Java JWT驗證程式,Spring Security和Pac4j對其進行保護。

使用Okta将OIDC安全性添加到Java REST API

您将需要在Okta中建立OIDC應用程式,以驗證将要實施的安全配置。 要使此操作毫不費力,您可以使用Okta的OIDC API。 在Okta,我們的目标是使身份管理比您以往更加輕松,安全和可擴充。 Okta是一項雲服務,允許開發人員建立,編輯和安全地存儲使用者帳戶和使用者帳戶資料,并将它們與一個或多個應用程式連接配接。 我們的API使您能夠:

  • 驗證和授權使用者
  • 存儲有關您的使用者的資料
  • 執行基于密碼的社交登入
  • 通過多因素身份驗證保護您的應用程式
  • 以及更多! 檢視我們的産品文檔

你賣了嗎 立即注冊一個永久免費的開發者帳戶 ! 完成後,請完成以下步驟以建立OIDC應用程式。

  1. 登入到您在developer.okta.com上的開發者帳戶。
  2. 導航到應用程式 ,然後單擊添加應用程式 。
  3. 選擇“ Web” ,然後單擊“ 下一步” 。
  4. 為應用程式命名(例如

    Java EE Secure API

    ),然後添加以下内容作為登入重定向URI:
    • http://localhost:3000/implicit/callback

    • http://localhost:8080/login/oauth2/code/okta

    • http://localhost:8080/callback?client_name=OidcClient

  5. 單擊完成 ,然後編輯項目并啟用“隐式(混合)”作為授予類型(允許ID和通路令牌),然後單擊儲存 。

使用JWT Verifier保護Java REST API

要從Okta驗證JWT,您需要将Okta Java JWT Verifier添加到

pom.xml

<properties>
    ...
    <okta-jwt.version>0.3.0</okta-jwt.version>
</properties>

<dependencies>
    ...
    <dependency>
        <groupId>com.okta.jwt</groupId>
        <artifactId>okta-jwt-verifier</artifactId>
        <version>${okta-jwt.version}</version>
    </dependency>
</dependencies>
           

然後建立一個

JwtFilter.java

(在

src/main/java/com/okta/developer

目錄中)。 該過濾器查找其中包含通路令牌的

authorization

标頭。 如果存在,它将對其進行驗證并列印出使用者的

sub

,也就是他們的電子郵件位址。 如果不存在或有效,則傳回拒絕通路狀态。

確定使用您建立的應用中的設定替換

{yourOktaDomain}

{clientId}

package com.okta.developer;

import com.nimbusds.oauth2.sdk.ParseException;
import com.okta.jwt.JoseException;
import com.okta.jwt.Jwt;
import com.okta.jwt.JwtHelper;
import com.okta.jwt.JwtVerifier;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "jwtFilter", urlPatterns = "/*")
public class JwtFilter implements Filter {
    private JwtVerifier jwtVerifier;

    @Override
    public void init(FilterConfig filterConfig) {
        try {
            jwtVerifier = new JwtHelper()
                    .setIssuerUrl("https://{yourOktaDomain}/oauth2/default")
                    .setClientId("{yourClientId}")
                    .build();
        } catch (IOException | ParseException e) {
            System.err.print("Configuring JWT Verifier failed!");
            e.printStackTrace();
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println("In JwtFilter, path: " + request.getRequestURI());

        // Get access token from authorization header
        String authHeader = request.getHeader("authorization");
        if (authHeader == null) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access denied.");
            return;
        } else {
            String accessToken = authHeader.substring(authHeader.indexOf("Bearer ") + 7);
            try {
                Jwt jwt = jwtVerifier.decodeAccessToken(accessToken);
                System.out.println("Hello, " + jwt.getClaims().get("sub"));
            } catch (JoseException e) {
                e.printStackTrace();
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access denied.");
                return;
            }
        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}
           

為確定此過濾器正常工作,請重新啟動您的應用并運作:

mvn package tomee:run
           

如果在浏覽器中導航到

http://localhost:8080/good-beers

,則會看到拒絕通路錯誤。

使用Java EE和OIDC建構Java REST API

為了證明它可以與有效的JWT一起使用,您可以克隆我的Bootiful React項目,并運作其UI:

git clone -b okta https://github.com/oktadeveloper/spring-boot-react-example.git bootiful-react
cd bootiful-react/client
npm install
           

編輯此項目的

client/src/App.tsx

檔案,并更改

issuer

clientId

以比對您的應用程式。

const config = {
  issuer: 'https://{yourOktaDomain}/oauth2/default',
  redirectUri: window.location.origin + '/implicit/callback',
  clientId: '{yourClientId}'
};
           

然後啟動它:

npm start
           

然後,您應該能夠使用建立帳戶所用的憑據登入

http://localhost:3000

。 但是,由于CORS錯誤(在浏覽器的開發人員控制台中),您将無法從API加載任何啤酒。

Failed to load http://localhost:8080/good-beers: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
           

提示:如果看到401并且沒有CORS錯誤,則可能意味着您的客戶ID不比對。

要解決此CORS錯誤,請在

JwtFilter.java

類旁邊添加一個

CorsFilter.java

。 下面的過濾器将允許

OPTIONS

請求,并向後發送通路控制标頭,以允許任何起源,GET方法和任何标頭。 我建議您在生産中使這些設定更加具體。

package com.okta.developer;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "corsFilter")
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println("In CorsFilter, method: " + request.getMethod());

        // Authorize (allow) all domains to consume the content
        response.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
        response.addHeader("Access-Control-Allow-Methods", "GET");
        response.addHeader("Access-Control-Allow-Headers", "*");

        // For HTTP OPTIONS verb/method reply with ACCEPTED status code -- per CORS handshake
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(HttpServletResponse.SC_ACCEPTED);
            return;
        }

        // pass the request along the filter chain
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) {
    }

    @Override
    public void destroy() {
    }
}
           

您添加的兩個過濾器都使用

@WebFilter

進行注冊。 這是一個友善的注釋,但不提供任何過濾器排序功能。 要解決此丢失的功能,請修改

JwtFilter

,使其

@WebFilter

中沒有

urlPattern

@WebFilter(filterName = "jwtFilter")
           

然後建立一個

src/main/webapp/WEB-INF/web.xml

檔案,并使用以下XML進行填充。 這些過濾器映射可確定

CorsFilter

處理

CorsFilter

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <filter-mapping>
        <filter-name>corsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>jwtFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
           

重新啟動Java API,現在一切正常!

使用Java EE和OIDC建構Java REST API

在控制台中,您應該看到類似于我的消息:

In CorsFilter, method: OPTIONS
In CorsFilter, method: GET
In JwtFilter, path: /good-beers
Hello, [email protected]
           

通過Okta的JWT驗證程式使用過濾器是實作資源伺服器的一種簡單方法(采用OAuth 2.0命名法)。 但是,它不向您提供有關該使用者的任何資訊。

JwtVerifier

接口的确有一個

decodeIdToken(String idToken, String nonce)

方法,但是您必須從用戶端傳遞ID令牌才能使用它。

在接下來的兩節中,我将向您展示如何使用Spring Security和Pac4j來實作類似的安全性。 作為獎勵,我将向您展示如何提示使用者登入(當他們嘗試直接通路API時)并擷取使用者的資訊。

通過Spring Security保護Java REST API

Spring Security是我在Javaland中最喜歡的架構之一。 在顯示如何使用Spring Security時,此部落格上的大多數示例都使用Spring Boot。 我将使用最新版本– 5.1.0.RC2 –是以本教程将保持最新狀态。

還原更改以添加JWT Verifier,或直接删除

web.xml

繼續。

修改您的

pom.xml

使其具有Spring Security所需的依賴關系。 您還需要添加Spring的快照存儲庫以擷取候選版本。

<properties>
    ...
    <spring-security.version>5.1.0.RC2</spring-security.version>
    <spring.version>5.1.0.RC3</spring.version>
    <jackson.version>2.9.6</jackson.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-bom</artifactId>
            <version>${spring-security.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    ...
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-resource-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-jose</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson.version}</version>
    </dependency>
</dependencies>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshot</name>
        <url>https://repo.spring.io/libs-snapshot</url>
    </repository>
</repositories>
           

src/main/java/com/okta/developer

建立一個

SecurityWebApplicationInitializer.java

類:

package com.okta.developer;

import org.springframework.security.web.context.*;

public class SecurityWebApplicationInitializer
   extends AbstractSecurityWebApplicationInitializer {

   public SecurityWebApplicationInitializer() {
       super(SecurityConfiguration.class);
   }
}
           

在同一目錄中建立一個

SecurityConfiguration.java

類。 此類使用Spring Security 5的

oauth2Login()

并向Spring Security注冊您的Okta應用程式。

package com.okta.developer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
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.oauth2.client.InMemoryOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.ClientRegistrations;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@Configuration
@EnableWebSecurity
@PropertySource("classpath:application.properties")
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    private final String clientSecret;
    private final String clientId;
    private final String issuerUri;

    @Autowired
    public SecurityConfiguration(@Value("${okta.issuer-uri}") String issuerUri,
            @Value("${okta.client-id}") String clientId,
            @Value("${okta.client-secret}") String clientSecret) {
        this.issuerUri = issuerUri;
        this.clientId = clientId;
        this.clientSecret = clientSecret;
    }

@Override
   protected void configure(HttpSecurity http) throws Exception {
      http
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
                .and()
           .csrf()
               .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
               .and()
           .authorizeRequests()
               .anyRequest().authenticated()
               .and()
           .oauth2Login();
   }

   @Bean
   public OAuth2AuthorizedClientService authorizedClientService() {
       return new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository());
   }

   @Bean
   public ClientRegistrationRepository clientRegistrationRepository() {
       List<ClientRegistration> registrations = clients.stream()
               .map(this::getRegistration)
               .filter(Objects::nonNull)
               .collect(Collectors.toList());

       return new InMemoryClientRegistrationRepository(registrations);
   }

   @Bean
    public ClientRegistrationRepository clientRegistrationRepository() {
        ClientRegistration okta = getRegistration();
        return new InMemoryClientRegistrationRepository(okta);
    }

    ClientRegistrations.fromOidcIssuerLocation(Objects.requireNonNull(issuerUri))
            .registrationId("okta")
            .clientId(clientId)
            .clientSecret(clientSecret)
            .build();
}
           

建立

src/main/resources/application.properties

并用Okta OIDC應用設定進行填充。

okta.client-id={clientId}
okta.client-secret={clientSecret}
okta.issuer-uri=https://{yourOktaDomain}/oauth2/default
           

感謝Baeldung提供有關Spring Security 5 OAuth的出色文檔 。

因為啟用了CSRF,是以必須在任何

<h:form>

标記内添加以下隐藏字段以保護CSRF。 我将以下内容添加到

src/main/webapp/beer.xhtml

result.xhtml

<input type="hidden" value="${_csrf.token}" name="${_csrf.parameterName}"/>
           

重新啟動您的API(

mvn clean package tomee:run

)并導航到

http://localhost:8080/good-beers

。 您應該重定向到Okta進行登入。

使用Java EE和OIDC建構Java REST API

輸入有效的憑證,您應該在浏覽器中看到JSON。 JSON Viewer Chrome插件提供了美觀的JSON。

使用Java EE和OIDC建構Java REST API

要求使用者登入以檢視您的API資料很友善,但是最好将其作為React UI示例的資源伺服器。 OAuth 2.0資源伺服器支援是Spring Security 5.1.0 RC1中的新增功能,是以我将向您展示如何使用它。

用以下代碼替換

SecurityConfiguration.java

configure()

方法,該代碼啟用CORS并設定資源伺服器。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
            .and()
        .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
        .cors()
            .and()
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .oauth2Login()
            .and()
        .oauth2ResourceServer()
            .jwt();
}

@Bean
JwtDecoder jwtDecoder() {
    return JwtDecoders.fromOidcIssuerLocation(this.issuerUri);
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.setAllowedOrigins(Collections.singletonList("http://localhost:3000"));
    configuration.setAllowedMethods(Collections.singletonList("GET"));
    configuration.setAllowedHeaders(Collections.singletonList("*"));
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}
           

進行這些更改之後,重新啟動您的API并确認您的React UI可以與之對話。 很漂亮吧?

Spring Security的使用者資訊

Spring Security與Servlet API內建在一起,是以您可以使用以下方法來擷取目前使用者的資訊。

  • HttpServletRequest.getRemoteUser()

  • HttpServletRequest.getUserPrincipal()

擁有

Principal

,您可以擷取有關使用者的詳細資訊,包括其角色(又名,權限)。

OAuth2Authentication authentication = (OAuth2Authentication) principal;
Map<String, Object> user = (Map<String, Object>) authentication.getUserAuthentication().getDetails();
           

請參閱Spring Security的Servlet API內建文檔以擷取更多資訊。

使用Pac4j鎖定Java REST API

我想向您展示的確定Java REST API安全的最後一種技術是使用Pac4j,特别是j2e-pac4j 。

恢複您的更改以添加Spring Security。

git reset --hard HEAD
           

編輯

pom.xml

以添加完成本節所需的Pac4j庫。

<properties>
    ...
    <pac4j-j2e.version>4.0.0</pac4j-j2e.version>
    <pac4j.version>3.0.0</pac4j.version>
</properties>

<dependencies>
    ...
    <dependency>
        <groupId>org.pac4j</groupId>
        <artifactId>j2e-pac4j</artifactId>
        <version>${pac4j-j2e.version}</version>
    </dependency>
    <dependency>
        <groupId>org.pac4j</groupId>
        <artifactId>pac4j-oidc</artifactId>
        <version>${pac4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.pac4j</groupId>
        <artifactId>pac4j-http</artifactId>
        <version>${pac4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.pac4j</groupId>
        <artifactId>pac4j-jwt</artifactId>
        <version>${pac4j.version}</version>
    </dependency>
</dependencies>
           

就像建立JWT Verifier一樣,建立一個

src/main/java/com/okta/developer/CorsFilter.java

package com.okta.developer;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "corsFilter")
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println("In CorsFilter, method: " + request.getMethod());

        // Authorize (allow) all domains to consume the content
        response.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
        response.addHeader("Access-Control-Allow-Methods", "GET");
        response.addHeader("Access-Control-Allow-Headers", "*");

        // For HTTP OPTIONS verb/method reply with ACCEPTED status code -- per CORS handshake
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(HttpServletResponse.SC_ACCEPTED);
            return;
        }

        // pass the request along the filter chain
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig config) {
    }

    @Override
    public void destroy() {
    }
}
           

在同一程式包中建立一個

SecurityConfigFactory.java

。 将用戶端ID,密鑰和域占位符替換為與OIDC應用程式比對的占位符。

package com.okta.developer;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.pac4j.core.client.Clients;
import org.pac4j.core.client.direct.AnonymousClient;
import org.pac4j.core.config.Config;
import org.pac4j.core.config.ConfigFactory;
import org.pac4j.core.credentials.TokenCredentials;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.http.client.direct.HeaderClient;
import org.pac4j.jwt.config.signature.RSASignatureConfiguration;
import org.pac4j.jwt.credentials.authenticator.JwtAuthenticator;
import org.pac4j.jwt.util.JWKHelper;
import org.pac4j.oidc.client.OidcClient;
import org.pac4j.oidc.config.OidcConfiguration;
import org.pac4j.oidc.profile.OidcProfile;

import java.io.IOException;
import java.net.URL;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SecurityConfigFactory implements ConfigFactory {
    private final JwtAuthenticator jwtAuthenticator = new JwtAuthenticator();
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public Config build(final Object... parameters) {
        System.out.print("Building Security configuration...\n");

        final OidcConfiguration oidcConfiguration = new OidcConfiguration();
        oidcConfiguration.setClientId("{yourClientId}");
        oidcConfiguration.setSecret("{yourClientSecret}");
        oidcConfiguration.setDiscoveryURI("https://{yourOktaDomain}/oauth2/default/.well-known/openid-configuration");
        oidcConfiguration.setUseNonce(true);
        final OidcClient<OidcProfile, OidcConfiguration> oidcClient = new OidcClient<>(oidcConfiguration);
        oidcClient.setAuthorizationGenerator((ctx, profile) -> {
            profile.addRole("ROLE_USER");
            return profile;
        });

        HeaderClient headerClient = new HeaderClient("Authorization", "Bearer ", (credentials, ctx) -> {
            String token = ((TokenCredentials) credentials).getToken();
            if (token != null) {
                try {
                    // Get JWK
                    URL keysUrl = new URL("https://{yourOktaDomain}/oauth2/default/v1/keys");
                    Map map = mapper.readValue(keysUrl, Map.class);
                    List keys = (ArrayList) map.get("keys");
                    String json = mapper.writeValueAsString(keys.get(0));

                    // Build key pair and validate token
                    KeyPair rsaKeyPair = JWKHelper.buildRSAKeyPairFromJwk(json);
                    jwtAuthenticator.addSignatureConfiguration(new RSASignatureConfiguration(rsaKeyPair));
                    CommonProfile profile = jwtAuthenticator.validateToken(token);
                    credentials.setUserProfile(profile);
                    System.out.println("Hello, " + profile.getId());
                } catch (IOException e) {
                    System.err.println("Failed to validate Bearer token: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        });

        final Clients clients = new Clients("http://localhost:8080/callback",
                oidcClient, headerClient, new AnonymousClient());
        return new Config(clients);
    }
}
           

如果

oidcClient

的代碼中的

oidcClient

嘗試直接通路您的API,将使使用者登入Okta。

headerClient

設定了資源伺服器,該資源伺服器根據使用者的通路令牌對使用者進行授權。

建立

src/main/webapp/WEB-INF/web.xml

來映射

CorsFilter

以及Pac4j的

CallbackFilter

SecurityFilter

。 您可以看到

SecurityFilter

通過其

configFactory

init-param連結到

SecurityConfigFactory

類。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>javaee-pac4j-demo</display-name>

    <absolute-ordering/>

    <filter-mapping>
        <filter-name>corsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>callbackFilter</filter-name>
        <filter-class>org.pac4j.j2e.filter.CallbackFilter</filter-class>
        <init-param>
            <param-name>defaultUrl</param-name>
            <param-value>/</param-value>
        </init-param>
        <init-param>
            <param-name>renewSession</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>multiProfile</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>callbackFilter</filter-name>
        <url-pattern>/callback</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>OidcFilter</filter-name>
        <filter-class>org.pac4j.j2e.filter.SecurityFilter</filter-class>
        <init-param>
            <param-name>configFactory</param-name>
            <param-value>com.okta.developer.SecurityConfigFactory</param-value>
        </init-param>
        <init-param>
            <param-name>clients</param-name>
            <param-value>oidcClient,headerClient</param-value>
        </init-param>
        <init-param>
            <param-name>authorizers</param-name>
            <param-value>securityHeaders</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>OidcFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
</web-app>
           

為了更好地可視化使用者資訊,您需要建立更多檔案。 這些與JSF相關的檔案是從j2e-pac4j-cdi-demo複制的。

注意:我試圖在TomEE上運作j2e-pac4j-cdi-demo (沒有

web.xml

),但是失敗并出現錯誤:

Filters cannot be added to context [] as the context has been initialised

,是以無法将

Filters cannot be added to context [] as the context has been initialised

。 當使用Payara Maven插件時,它确實起作用。

建立

src/main/java/com/okta/developer/ProfileView.java

,這是一個JSF托管的bean,用于收集使用者的資訊。

package com.okta.developer;

import org.pac4j.core.context.WebContext;
import org.pac4j.core.profile.ProfileManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * Managed bean which exposes the pac4j profile manager.
 *
 * JSF views such as facelets can reference this to view the contents of profiles.
 *
 * @author Phillip Ross
 */
@Named
@RequestScoped
public class ProfileView {

    /** The static logger instance. */
    private static final Logger logger = LoggerFactory.getLogger(ProfileView.class);

    /** The pac4j web context. */
    @Inject
    private WebContext webContext;

    /** The pac4j profile manager. */
    @Inject
    private ProfileManager profileManager;

    /** Simple no-args constructor. */
    public ProfileView() {
    }

    /**
     * Gets the first profile (if it exists) contained in the profile manager.
     *
     * @return a list of pac4j profiles
     */
    public Object getProfile() {
        return profileManager.get(true).orElse(null); // It's fine to return a null reference if there is no value present.
    }

    /**
     * Gets the profiles contained in the profile manager.
     *
     * @return a list of pac4j profiles
     */
    public List getProfiles() {
        return profileManager.getAll(true);
    }

    /** Simply prints some debugging information post-construction. */
    @PostConstruct
    public void init() {
        logger.debug("webContext is null? {}", (webContext == null));
        logger.debug("profileManager is null? {}", (profileManager == null));
    }
}
           

src/main/webapp/oidc/index.xhtml

為JSF模闆。

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/WEB-INF/template.xhtml">
    <ui:define name="title">Pac4J Java EE Demo - Protected Area</ui:define>
    <ui:define name="content">
        <div class="ui-g">
            <div class="ui-g-12">
                <div class="ui-container">
                    <h1>Protected Area</h1>
                    <p><h:link value="Back" outcome="/index"/></p>
                </div>
                <ui:include src="/WEB-INF/facelets/includes/pac4j-profiles-list.xhtml"/>
            </div>
        </div>
    </ui:define>
</ui:composition>
           

建立

pac4j-profiles-list.xhtml

檔案,該檔案包含在

WEB-INF/facelets/includes

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets">
    <div class="ui-container">
        <p>Found  <h:outputText value="#{profileView.profiles.size()}"/> profiles.</p>
        <h:panelGroup layout="block" rendered="#{profileView.profiles.size() > 0}">
            <p>First profile:  <h:outputText value="#{profileView.profile}"/></p>
        </h:panelGroup>
    </div>

    <h:panelGroup layout="block" rendered="#{not empty profileView.profile}">
        <h2>Profile Details</h2>
        <p><h:outputText value="Id: #{profileView.profile.id}"/></p>
        <p><h:outputText value="Type Id: #{profileView.profile.typedId}"/></p>
        <p><h:outputText value="Remembered: #{profileView.profile.remembered}"/></p>
        <h3>Attributes (<h:outputText value="#{profileView.profile.attributes.size()}"/>)</h3>
        <h:panelGroup layout="block" rendered="#{profileView.profile.attributes.size() > 0}">
            <ul>
                <ui:repeat value="#{profileView.profile.attributes.keySet().toArray()}" var="attributeName">
                    <li><h:outputText value="#{attributeName}"/>: <h:outputText value="#{profileView.profile.attributes.get(attributeName)}"/> </li>
                </ui:repeat>
            </ul>
        </h:panelGroup>
        <h3>Roles (<h:outputText value="#{profileView.profile.roles.size()}"/>)</h3>
        <h:panelGroup layout="block" rendered="#{profileView.profile.roles.size() > 0}">
            <ul>
                <ui:repeat value="#{profileView.profile.roles.toArray()}" var="role">
                    <li><h:outputText value="#{role}"/></li>
                </ui:repeat>
            </ul>
        </h:panelGroup>
        <h3>Permissions (<h:outputText value="#{profileView.profile.permissions.size()}"/>)</h3>
        <h:panelGroup layout="block" rendered="#{profileView.profile.permissions.size() > 0}">
            <ul>
                <ui:repeat value="#{profileView.profile.permissions.toArray()}" var="permission">
                    <li><h:outputText value="#{permission}"/></li>
                </ui:repeat>
            </ul>
        </h:panelGroup>
    </h:panelGroup>
</ui:composition>
           

oidc/index.xhtml

模闆使用

WEB-INF/template.xhtml

,是以您也需要建立它。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:head>
        <f:facet name="first">
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
            <meta name="apple-mobile-web-app-capable" content="yes" />
        </f:facet>
        <title><ui:insert name="title">Pac4J Java EE Demo</ui:insert></title>
        <ui:insert name="head"/>
    </h:head>

    <h:body styleClass="main-body">
        <div class="layout-wrapper">
            <div class="layout-main">
                <ui:insert name="content"/>
            </div>
        </div>
    </h:body>
</html>
           

添加這些檔案後,重建項目并重新啟動TomEE。

mvn clean package tomee:run
           

導航到

http://localhost:8080/oidc/index.jsf

,您将被重定向到Okta進行登入。 如果您初次嘗試無法解決問題,請重新啟動浏覽器并使用隐身視窗。 您應該看到使用者的個人資料資訊。

使用Java EE和OIDC建構Java REST API

http://localhost:3000

嘗試您的React用戶端; 它也應該工作!

使用Java EE和OIDC建構Java REST API

如果您想知道為什麼不堆疊圖像,那是因為我将React應用程式的

BeerList.tsx

的啤酒清單的JSX更改為内聯。

<h2>Beer List</h2>
{beers.map((beer: Beer) =>
  <span key={beer.id} style={{float: 'left', marginRight: '10px', marginLeft: '10px'}}>
    {beer.name}<br/>
    <GiphyImage name={beer.name}/>
  </span>
)}
           

雅加達EE呢?

您可能已經聽說Java EE已經成為開源的(類似于Java SE的OpenJDK ),其新名稱為Jakarta EE 。 David Blevins是一個很好的朋友,并且積極參與Java EE / Jakarta EE。 有關證明,請參閱他的Twitter傳記:Apache TomEE,OpenEJB和Geronimo項目的創始人。 Apache,JCP EC,EE4J PMC,Jakarta EE WG,MicroProfile和Eclipse Board的成員。 首席執行官@Tomitribe 。

我問戴維何時會釋出可用的Jakarta EE。

David:目前的主要重點是建立與Java EE 8相容的Jakarta EE版本。我們希望在今年年底之前将其釋出。 釋出之後,我們将開始開發Jakarta EE 9并根據需要進行疊代。

Jakarta EE有一個工作組來決定平台的方向。

了解有關安全REST API,Java EE,Jakarta EE和OIDC的更多資訊

我希望您喜歡這個遊覽,向您展示了如何使用JWT和OIDC建構和保護Java EE REST API。 如果您想檢視每個完成部分的源代碼,我将它們放在GitHub repo的分支中。 您可以使用以下指令克隆不同的實作:

git clone -b jwt-verifier https://github.com/oktadeveloper/okta-java-ee-rest-api-example.git
git clone -b spring-security https://github.com/oktadeveloper/okta-java-ee-rest-api-example.git
git clone -b pac4j https://github.com/oktadeveloper/okta-java-ee-rest-api-example.git
           

如前所述,我們在此部落格上獲得的大多數Java教程都展示了如何使用Spring Boot。 如果您有興趣學習Spring Boot,這裡有一些我寫的教程将向您展示要點。

  • Spring Boot,OAuth 2.0和Okta入門
  • 使用React和Spring Boot建構一個簡單的CRUD應用
  • 使用Angular 7.0和Spring Boot 2.1建構基本的CRUD應用

如果您是OIDC的新手,建議您檢視以下文章:

  • Spring Security 5.0和OIDC入門
  • 身份,聲明和令牌– OpenID Connect入門,第1部分,共3部分
  • 行動中的OIDC – OpenID Connect入門,第2部分,共3部分
  • 令牌中有什麼? – OpenID Connect入門,第3部分,共3部分

有關Java REST API和TomEE的更多資訊,我建議以下來源:

  • David Blevins –解構REST安全,疊代2018
  • Antonio Goncalves –使用JWT保護JAX-RS端點
  • TomEE:使用Systemd運作

如果您到目前為止已經做到了,我懷疑您可能對以後的部落格文章感興趣。 在Twitter上關注我和我的整個團隊 , 在Facebook上關注我們,或者檢視我們的YouTube頻道 。 如有疑問,請在下面發表評論,或将其釋出到我們的開發者論壇 。

“我喜歡編寫身份驗證和授權代碼。” 〜從來沒有Java開發人員。

厭倦了一次又一次地建立相同的登入螢幕? 嘗試使用Okta API進行托管身份驗證,授權和多因素身份驗證。

使用Java EE和OIDC建構Java REST API最初于2018年9月12日釋出在Okta開發人員部落格上。

翻譯自: https://www.javacodegeeks.com/2018/10/build-java-rest-api-java-ee-oidc.html