Skip to content

Commit

Permalink
Merge pull request HideyoshiSolutions#38 from HideyoshiNakazone/imple…
Browse files Browse the repository at this point in the history
…ments-health-check-api

Implements Health Checker API and Formats Code
  • Loading branch information
HideyoshiNakazone committed Feb 16, 2024
2 parents e71f976 + 8c20569 commit 1e88486
Show file tree
Hide file tree
Showing 42 changed files with 155 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
@SpringBootApplication
public class BackendPortfolioApplication {

public static void main(String[] args) {
SpringApplication.run(BackendPortfolioApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(BackendPortfolioApplication.class, args);
}

@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hideyoshi.backendportfolio.base.config;

import antlr.actions.python.CodeLexer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class DefaultUserConfig {
CommandLineRunner run(UserService userService, UserRepository userRepo) {
return args -> {
UserDTO defaultUser = UserDTO.builder()
.name(ADMIN_NAME)
.email(ADMIN_EMAIL)
.username(ADMIN_USERNAME)
.password(ADMIN_PASSWORD)
.provider(Provider.LOCAL)
.roles(new ArrayList<>())
.build();
.name(ADMIN_NAME)
.email(ADMIN_EMAIL)
.username(ADMIN_USERNAME)
.password(ADMIN_PASSWORD)
.provider(Provider.LOCAL)
.roles(new ArrayList<>())
.build();
if (!userRepo.findByUsername(defaultUser.getUsername()).isPresent()) {
defaultUser = userService.saveUser(defaultUser);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hideyoshi.backendportfolio.base.config;

import com.hideyoshi.backendportfolio.util.exception.AuthenticationInvalidException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -15,7 +14,7 @@

@Log4j2
@Component("restAuthenticationEntryPoint")
public class RestAuthenticationEntryPointConfig implements AuthenticationEntryPoint{
public class RestAuthenticationEntryPointConfig implements AuthenticationEntryPoint {

@Autowired
@Qualifier("handlerExceptionResolver")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import com.hideyoshi.backendportfolio.util.exception.AuthenticationInvalidException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -24,7 +22,6 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.filter.ForwardedHeaderFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -69,31 +66,31 @@ private void addSecurityToHttp(HttpSecurity http) throws Exception {
customAuthenticationFilter.setFilterProcessesUrl("/user/login");

http.authorizeRequests()
.antMatchers("/session/**").permitAll()
.and().authorizeRequests().antMatchers("/user/signup").permitAll()
.and().authorizeRequests().antMatchers("/user/oauth/**").permitAll()
.and().authorizeRequests().antMatchers("/user/login/**").permitAll()
.and().authorizeRequests().antMatchers("/**").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")
.antMatchers("/session/**").permitAll()
.and().authorizeRequests().antMatchers("/user/signup").permitAll()
.and().authorizeRequests().antMatchers("/user/oauth/**").permitAll()
.and().authorizeRequests().antMatchers("/user/login/**").permitAll()
.and().authorizeRequests().antMatchers("/**").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")

.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and().addFilter(customAuthenticationFilter)
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and().addFilter(customAuthenticationFilter)

.addFilterBefore(new CustomAuthorizationFilter(this.authService), UsernamePasswordAuthenticationFilter.class);
.addFilterBefore(new CustomAuthorizationFilter(this.authService), UsernamePasswordAuthenticationFilter.class);

}

private void addOAuthSecurityToHttp(HttpSecurity http) throws Exception {

http.oauth2Login()
.authorizationEndpoint()
.authorizationRequestRepository(this.oAuthRequestRepository)
.and().successHandler(this::successHandler)
.failureHandler(this::failureHandler);
.authorizationEndpoint()
.authorizationRequestRepository(this.oAuthRequestRepository)
.and().successHandler(this::successHandler)
.failureHandler(this::failureHandler);
}

private void successHandler(HttpServletRequest request,
HttpServletResponse response,
Authentication authentication ) throws IOException {
Authentication authentication) throws IOException {

OAuth2User oauthUser = (OAuth2User) authentication.getPrincipal();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.hideyoshi.backendportfolio.base.security.filter;

import com.auth0.jwt.algorithms.Algorithm;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hideyoshi.backendportfolio.base.config.RestAuthenticationEntryPointConfig;
import com.hideyoshi.backendportfolio.base.security.service.AuthService;
import com.hideyoshi.backendportfolio.base.user.model.TokenDTO;
import com.hideyoshi.backendportfolio.base.user.model.UserDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
Expand All @@ -17,11 +13,7 @@
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@Log4j2
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
Expand Down Expand Up @@ -65,5 +57,5 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

public class CustomAuthorizationFilter extends OncePerRequestFilter {

public static String AUTHORIZATION_TYPE_STRING = "Bearer ";
private static final List<String> notProtectedPaths = Arrays.asList(
"/user/login",
"/user/signup",
"/user/login/refresh"
);

private static final String AUTHORIZATION_TYPE_STRING = "Bearer ";

private final AuthService authService;

Expand Down Expand Up @@ -62,11 +68,6 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}

private Boolean isPathNotProtected(String path) {

List<String> notProtectedPaths = Arrays.asList(
"/user/login"
);

return notProtectedPaths.contains(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Component
@RequiredArgsConstructor
public class ConfigInterceptor implements WebMvcConfigurer {
public class ConfigInterceptor implements WebMvcConfigurer {

private final UserResourceAccessInterceptor userResourceAccessInterceptor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean preHandle(final HttpServletRequest request, final HttpServletResp
return true;
}

final UserResourceGuard annotation = ((HandlerMethod)handler)
final UserResourceGuard annotation = ((HandlerMethod) handler)
.getMethodAnnotation(UserResourceGuard.class);

if (Objects.nonNull(annotation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.Builder;
import lombok.Data;

import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.hideyoshi.backendportfolio.base.user.entity.Provider;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.core.user.OAuth2User;

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.hideyoshi.backendportfolio.base.user.entity.Provider;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.core.user.OAuth2User;

@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hideyoshi.backendportfolio.base.security.oauth.mapper;

import com.hideyoshi.backendportfolio.base.user.entity.Provider;
import org.springframework.security.oauth2.core.user.OAuth2User;

public interface OAuthMap {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ private OAuthMapper(Class oAuthMap, Provider provider) {
this.provider = provider;
}

public Class getMap() {
return oAuthMap;
}

public static OAuthMapper byValue(String name) {
for (OAuthMapper e : values()) {
if (e.getProvider().getName().equals(name)) {
Expand All @@ -32,4 +28,8 @@ public static OAuthMapper byValue(String name) {
throw new IllegalArgumentException("Argument not valid.");
}

public Class getMap() {
return oAuthMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationReq
String state = authorizationRequest.getState();

request.getSession().setAttribute(
String.format("state_%s", state),
authorizationRequest
String.format("state_%s", state),
authorizationRequest
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface AuthService {

TokenDTO generateRefreshToken(@Valid UserDTO user, Algorithm algorithm, HttpServletRequest request);

HashMap<String,TokenDTO> generateTokens(@Valid UserDTO user, Algorithm algorithm, HttpServletRequest request);
HashMap<String, TokenDTO> generateTokens(@Valid UserDTO user, Algorithm algorithm, HttpServletRequest request);

UsernamePasswordAuthenticationToken verifyAccessToken(String authorizationHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,15 @@
public class AuthServiceImpl implements AuthService {


private static final String AUTHORIZATION_TYPE_STRING = "Bearer ";
private final UserService userService;
private final StorageService storageService;
@Value("${com.hideyoshi.tokenSecret}")
private String TOKEN_SECRET;

@Value("${com.hideyoshi.accessTokenDuration}")
private Integer ACCESS_TOKEN_DURATION;

@Value("${com.hideyoshi.refreshTokenDuration}")
private Integer REFRESH_TOKEN_DURATION;

private static final String AUTHORIZATION_TYPE_STRING = "Bearer ";

private final UserService userService;

private final StorageService storageService;

@Autowired
@Qualifier("handlerExceptionResolver")
private HandlerExceptionResolver resolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.hideyoshi.backendportfolio.base.security.model.AuthDTO;
import com.hideyoshi.backendportfolio.base.session.service.SessionManagerService;
import com.hideyoshi.backendportfolio.base.user.model.UserDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

Expand All @@ -24,7 +26,7 @@ public ResponseEntity<AuthDTO> validateCurrentSession(HttpSession session) {
return ResponseEntity.ok(this.sessionManagerService.validateSession(session));
}

@DeleteMapping(path="/destroy")
@DeleteMapping(path = "/destroy")
public ResponseEntity<Void> destroyCurrentSession(HttpSession session) {
this.sessionManagerService.destroySession(session);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hideyoshi.backendportfolio.base.session.service;

import com.hideyoshi.backendportfolio.base.security.model.AuthDTO;
import com.hideyoshi.backendportfolio.base.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public enum Provider {
this.name = name;
}

public String getName() {
return name;
}

public static Provider byValue(String name) {
for (Provider p : values()) {
if (p.getName().equals(name)) {
Expand All @@ -27,4 +23,8 @@ public static Provider byValue(String name) {
throw new IllegalArgumentException("Argument not valid.");
}

public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public enum Role {
this.description = description;
}

public String getDescription() {
return this.description;
}

public static Role byValue(String description) {
for (Role r : values()) {
if (r.getDescription().equals(description)) {
Expand All @@ -26,4 +22,8 @@ public static Role byValue(String description) {
throw new IllegalArgumentException("Argument not valid.");
}

public String getDescription() {
return this.description;
}

}
Loading

0 comments on commit 1e88486

Please sign in to comment.