Skip to content

Commit

Permalink
adjust /authenticate api
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 23, 2024
1 parent ca5ecee commit 724442e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 71 deletions.
9 changes: 9 additions & 0 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
"listOrFlux": "List",
"lowercaseBaseName": "jhipster",
"mainClass": "JhipsterApp",
"mainJavaDir": "src/main/java/",
Expand Down Expand Up @@ -682,6 +683,8 @@ exports[`generator - app with default config should match snapshot 1`] = `
},
"nodePackageManager": "npm",
"nodeVersion": "NODE_VERSION",
"optionalOrMono": "Optional",
"optionalOrMonoOfNullable": "Optional.ofNullable",
"packageFolder": "com/mycompany/myapp/",
"packageInfoJavadocs": [
{
Expand Down Expand Up @@ -1197,6 +1200,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
"listOrFlux": "Flux",
"lowercaseBaseName": "jhipster",
"mainClass": "JhipsterApp",
"mainJavaDir": "src/main/java/",
Expand Down Expand Up @@ -1294,6 +1298,8 @@ exports[`generator - app with gateway should match snapshot 1`] = `
},
"nodePackageManager": "npm",
"nodeVersion": "NODE_VERSION",
"optionalOrMono": "Mono",
"optionalOrMonoOfNullable": "Mono.justOrEmpty",
"packageFolder": "com/mycompany/myapp/",
"packageInfoJavadocs": [
{
Expand Down Expand Up @@ -1815,6 +1821,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
],
"liquibaseAddH2Properties": false,
"liquibaseDefaultSchemaName": "",
"listOrFlux": "List",
"lowercaseBaseName": "jhipster",
"mainClass": "JhipsterApp",
"mainJavaDir": "src/main/java/",
Expand Down Expand Up @@ -1853,6 +1860,8 @@ exports[`generator - app with microservice should match snapshot 1`] = `
},
"nodePackageManager": "npm",
"nodeVersion": "NODE_VERSION",
"optionalOrMono": "Optional",
"optionalOrMonoOfNullable": "Optional.ofNullable",
"packageFolder": "com/mycompany/myapp/",
"packageInfoJavadocs": [
{
Expand Down
7 changes: 7 additions & 0 deletions generators/java/generators/bootstrap/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ export default class BootstrapGenerator extends BaseApplicationGenerator {
...editFileCallback,
);
},
imperativeOrReactive({ applicationDefaults }) {
applicationDefaults({
optionalOrMono: ({ reactive }) => (reactive ? 'Mono' : 'Optional'),
optionalOrMonoOfNullable: ({ reactive }) => (reactive ? 'Mono.justOrEmpty' : 'Optional.ofNullable'),
listOrFlux: ({ reactive }) => (reactive ? 'Flux' : 'List'),
});
},
});
}

Expand Down
3 changes: 3 additions & 0 deletions generators/server/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ export type SpringBootApplication = JavaApplication &
skipCheckLengthOfIdentifier: boolean;

imperativeOrReactive: string;
optionalOrMono: string;
optionalOrMonoOfNullable: string;
listOrFlux: string;
generateAuthenticationApi?: boolean;
generateInMemoryUserCredentials?: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
-%>
package <%= packageName %>.web.rest;

<%_ if (authenticationTypeSession && !reactive) { _%>
<%_ if (authenticationTypeSession && !reactive) { _%>
import <%= packageName %>.domain.PersistentToken;
import <%= packageName %>.repository.PersistentTokenRepository;
<%_ } _%>
<%_ if (!reactive) { _%>
<%_ } _%>
<%_ if (!reactive) { _%>
import <%= user.entityAbsoluteClass %>;
<%_ } _%>
<%_ } _%>
import <%= packageName %>.repository.UserRepository;
import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.service.MailService;
Expand All @@ -35,31 +35,23 @@ import <%= packageName %>.web.rest.errors.*;
import <%= packageName %>.web.rest.vm.KeyAndPasswordVM;
import <%= packageName %>.web.rest.vm.ManagedUserVM;

import java.security.Principal;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
<%_ if (reactive) { _%>
import org.springframework.web.server.ServerWebExchange;
<%_ if (reactive) { _%>
import java.util.Objects;
import reactor.core.publisher.Mono;
<%_ } _%>

<%_ if (!reactive && !authenticationTypeJwt) { _%>
import jakarta.servlet.http.HttpServletRequest;
<%_ } _%>
<%_ } else { _%>
import java.util.*;
<%_ } _%>
import jakarta.validation.Valid;
<%_ if (authenticationTypeSession && !reactive) { _%>
<%_ if (authenticationTypeSession && !reactive) { _%>
import java.nio.charset.StandardCharsets;
import java.net.URLDecoder;
<%_ } _%>
<%_ if (reactive) { _%>
import java.security.Principal;
import java.util.Objects;
<%_ } _%>
<%_ if (!reactive) { _%>
import java.util.*;
<%_ } _%>
<%_ } _%>

/**
* REST controller for managing the current user's account.
Expand Down Expand Up @@ -144,19 +136,13 @@ public class AccountResource {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}

<%_ } _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.service.UserService;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;

import java.security.Principal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
<%_ if (reactive) { _%>
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
<%_ } else { _%>
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
<%_ } _%>

import java.security.Principal;

/**
* REST controller for managing the current user's account.
*/
Expand Down Expand Up @@ -82,18 +80,12 @@ public class AccountResource {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,21 @@ import <%= packageName %>.security.SecurityUtils;
import <%= packageName %>.domain.Authority;

<%_ } _%>

import java.security.Principal;
<%_ if (reactive) { _%>
import reactor.core.publisher.Mono;
<%_ } else { _%>
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
<%_ } _%>

<%_ if (authenticationTypeOauth2) { _%>
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
<%_ if (reactive) { _%>
import org.springframework.web.server.ServerWebExchange;
<%_ } _%>
<%_ } _%>
<%_ if (authenticationTypeOauth2 || authenticationTypeJwt) { _%>
import java.security.Principal;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.security.authentication.AbstractAuthenticationToken;
<%_ } else { _%>
<%_ if (reactive) { _%>
import java.security.Principal;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.userdetails.UserDetails;
Expand Down Expand Up @@ -147,19 +142,13 @@ public class AccountResource {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}
<%_ } _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ import org.springframework.security.oauth2.jwt.JwtClaimsSet;
import org.springframework.security.oauth2.jwt.JwtEncoder;
import org.springframework.security.oauth2.jwt.JwtEncoderParameters;
import org.springframework.web.bind.annotation.*;
<%_ if (reactive) { _%>
import java.security.Principal;
<%_ if (reactive) { _%>
import reactor.core.publisher.Mono;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
<%_ } else { _%>
import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.context.SecurityContextHolder;
<%_ } _%>
Expand Down Expand Up @@ -120,19 +119,13 @@ public class AuthenticateController {
/**
* {@code GET /authenticate} : check if the user is authenticated, and return its login.
*
* @param request the HTTP request.
* @param principal the authentication principal.
* @return the login if the user is authenticated.
*/
@GetMapping("/authenticate")
<%_ if (reactive) { _%>
public Mono<String> isAuthenticated(ServerWebExchange request) {
public <%- optionalOrMono %><String> isAuthenticated(Principal principal) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getPrincipal().map(Principal::getName);
<%_ } else { _%>
public String isAuthenticated(HttpServletRequest request) {
LOG.debug("REST request to check if the current user is authenticated");
return request.getRemoteUser();
<%_ } _%>
return <%- optionalOrMonoOfNullable %>(principal).map(Principal::getName);
}

public String createToken(Authentication authentication, boolean rememberMe) {
Expand Down

0 comments on commit 724442e

Please sign in to comment.