2021-08-08
This commit is contained in:
27
exception/build.gradle.kts
Normal file
27
exception/build.gradle.kts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Spring-boot Examples
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id("elex-spring-boot")
|
||||||
|
|
||||||
|
id("org.springframework.boot") version "2.5.3"
|
||||||
|
id("io.spring.dependency-management") version "1.0.11.RELEASE"
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||||
|
implementation("org.springframework.boot:spring-boot-starter-mustache")
|
||||||
|
|
||||||
|
compileOnly("org.projectlombok:lombok")
|
||||||
|
developmentOnly("org.springframework.boot:spring-boot-devtools")
|
||||||
|
|
||||||
|
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
|
||||||
|
annotationProcessor("org.projectlombok:lombok")
|
||||||
|
|
||||||
|
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||||
|
|
||||||
|
}
|
||||||
20
exception/src/main/java/kr/pe/elex/examples/Application.java
Normal file
20
exception/src/main/java/kr/pe/elex/examples/Application.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Spring-boot Examples
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Application {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Application.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* Spring-boot Examples
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.ContentDisposition;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Controller
|
||||||
|
public class MyController {
|
||||||
|
@Autowired
|
||||||
|
private MyService service;
|
||||||
|
|
||||||
|
@GetMapping(path = {"/m1"})
|
||||||
|
public String index() throws Exception {
|
||||||
|
service.doSomething("illegal");
|
||||||
|
return "main";
|
||||||
|
}
|
||||||
|
@GetMapping(path = {"/m2"})
|
||||||
|
public String index2() throws Exception {
|
||||||
|
service.doSomething("exception");
|
||||||
|
return "main";
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(IllegalArgumentException.class)
|
||||||
|
public ResponseEntity<?> handleException(Throwable e){
|
||||||
|
log.debug("Handled by {}.", getClass().getName());
|
||||||
|
HashMap<String,Object> resp = new HashMap<>();
|
||||||
|
resp.put("message",e.getMessage());
|
||||||
|
return ResponseEntity.badRequest().body(resp);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class MyExceptionHandler {
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<?> handleException(Throwable e){
|
||||||
|
log.debug("Handled by {}.", getClass().getName());
|
||||||
|
HashMap<String,Object> resp = new HashMap<>();
|
||||||
|
resp.put("message",e.getMessage());
|
||||||
|
return ResponseEntity.badRequest().body(resp);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
exception/src/main/java/kr/pe/elex/examples/MyService.java
Normal file
14
exception/src/main/java/kr/pe/elex/examples/MyService.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class MyService {
|
||||||
|
public String doSomething(String param) throws IllegalArgumentException, Exception {
|
||||||
|
if ("illegal" == param) throw new IllegalArgumentException("Oops~");
|
||||||
|
if ("exception" == param) throw new Exception("Oops~");
|
||||||
|
return "Hello";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/*
|
||||||
|
* Spring-boot Examples
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
* https://www.elex-project.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
package kr.pe.elex.examples;
|
||||||
9
exception/src/main/resources/application.yaml
Normal file
9
exception/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: My spring-boot project
|
||||||
|
servlet:
|
||||||
|
multipart:
|
||||||
|
max-file-size: 128KB
|
||||||
|
max-request-size: 128KB
|
||||||
|
server:
|
||||||
|
port: 8080
|
||||||
10
exception/src/main/resources/banner.txt
Normal file
10
exception/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
('-. ('-. ) (`-.
|
||||||
|
_( OO) _( OO) ( OO ).
|
||||||
|
(,------.,--. (,------.(_/. \_)-.
|
||||||
|
| .---'| |.-') | .---' \ `.' /
|
||||||
|
| | | | OO ) | | \ /\
|
||||||
|
(| '--. | |`-' |(| '--. \ \ |
|
||||||
|
| .--'(| '---.' | .--' .' \_)
|
||||||
|
| `---.| | | `---. / .'. \
|
||||||
|
`------'`------' `------''--' '--'
|
||||||
|
powered by ELEX
|
||||||
48
exception/src/main/resources/logback-spring.xml
Normal file
48
exception/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
~ Spring-boot Examples
|
||||||
|
~
|
||||||
|
~ Copyright (c) 2021. Elex. All Rights Reserved.
|
||||||
|
~ https://www.elex-project.com/
|
||||||
|
-->
|
||||||
|
|
||||||
|
<configuration>
|
||||||
|
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||||
|
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
|
||||||
|
|
||||||
|
<springProperty name="LOG_DIR" source="logging.path"
|
||||||
|
defaultValue="${user.home}/logs"/>
|
||||||
|
<property name="LOG_PATH" value="${LOG_DIR}/stephanie.log"/>
|
||||||
|
|
||||||
|
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="ROLLING-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
|
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||||
|
<charset>UTF-8</charset>
|
||||||
|
<pattern>${FILE_LOG_PATTERN}</pattern>
|
||||||
|
</encoder>
|
||||||
|
<file>${LOG_PATH}</file>
|
||||||
|
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||||
|
<fileNamePattern>${LOG_DIR}/sebastian_%d{yyyy-MM-dd}_%i.log.gz</fileNamePattern>
|
||||||
|
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||||
|
<maxFileSize>10MB</maxFileSize>
|
||||||
|
</timeBasedFileNamingAndTriggeringPolicy>
|
||||||
|
<maxHistory>60</maxHistory>
|
||||||
|
</rollingPolicy>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="kr.pe.elex" level="debug" additivity="false">
|
||||||
|
<appender-ref ref="CONSOLE"/>
|
||||||
|
<appender-ref ref="ROLLING-FILE"/>
|
||||||
|
</logger>
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="CONSOLE"/>
|
||||||
|
<appender-ref ref="ROLLING-FILE"/>
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
||||||
15
exception/src/main/resources/templates/main.mustache
Normal file
15
exception/src/main/resources/templates/main.mustache
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<h1>File Upload Example!!!</h1>
|
||||||
|
<form action="/upload" method="POST" enctype="multipart/form-data">
|
||||||
|
<label for="file">Select a file: </label>
|
||||||
|
<input id="file" name="file" type="file" title="Upload"/>
|
||||||
|
<div>
|
||||||
|
<button>Upload</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
<h2>Uploaded Files</h2>
|
||||||
|
<p>Click to download.</p>
|
||||||
|
<ul>
|
||||||
|
{{#files}}
|
||||||
|
<li><a href="/files/{{.}}">{{.}}</a></li>
|
||||||
|
{{/files}}
|
||||||
|
</ul>
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.*;
|
||||||
|
import io.jsonwebtoken.security.SignatureException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
//@RestControllerAdvice
|
||||||
|
@Deprecated
|
||||||
|
public class JwtExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler({IncorrectClaimException.class, MissingClaimException.class, ExpiredJwtException.class,
|
||||||
|
SignatureException.class, MalformedJwtException.class, UnsupportedJwtException.class})
|
||||||
|
public ResponseEntity<?> onEx(Throwable e) {
|
||||||
|
log.error("Oops~!!!", e);
|
||||||
|
|
||||||
|
Map<String, Object> data = new HashMap<>();
|
||||||
|
data.put("message", e.getMessage());
|
||||||
|
data.put("errorCode", HttpServletResponse.SC_BAD_REQUEST);
|
||||||
|
return ResponseEntity.badRequest().body(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
//@Component
|
||||||
|
@Deprecated
|
||||||
|
public class JwtInterceptor implements HandlerInterceptor {
|
||||||
|
@Autowired
|
||||||
|
private JwtService jwtService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
//final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||||
|
log.info("Interceptor Triggered!! {}", getClass().getName());
|
||||||
|
final String authHeader = request.getHeader("Authorization");
|
||||||
|
if (null != authHeader) {
|
||||||
|
Authentication authentication = jwtService.getAuthentication(authHeader);
|
||||||
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
//return HandlerInterceptor.super.preHandle(request, response, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package kr.pe.elex.examples;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class SampleInterceptor implements HandlerInterceptor {
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
log.info("Interceptor triggered!!! {}", getClass().getName());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -37,7 +37,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
http.authorizeRequests()
|
http.authorizeRequests()
|
||||||
.antMatchers("/", "/info", "/h2-console").permitAll() // 아무나 접근 가능
|
.antMatchers("/", "/info", "/h2-console").permitAll() // 아무나 접근 가능
|
||||||
.antMatchers("/h2-console/**").permitAll() // H2콘솔을 쓰기 위해 추가했음
|
.antMatchers("/h2-console/**").permitAll() // H2콘솔을 쓰기 위해 추가했음
|
||||||
.antMatchers("/api/signin").permitAll()
|
.antMatchers("/api/signin", "/hello/**").permitAll()
|
||||||
.antMatchers("/api/**").authenticated()
|
.antMatchers("/api/**").authenticated()
|
||||||
|
|
||||||
//.antMatchers("/admin").hasAnyRole("ADMIN")
|
//.antMatchers("/admin").hasAnyRole("ADMIN")
|
||||||
@@ -62,6 +62,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
|
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class)
|
||||||
.sessionManagement()
|
.sessionManagement()
|
||||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,12 +13,17 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
public class WebConfig implements WebMvcConfigurer {
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
//@Autowired
|
||||||
|
//private JwtInterceptor jwtInterceptor;
|
||||||
/*
|
/*
|
||||||
@Override
|
@Override
|
||||||
public void addViewControllers(@NotNull ViewControllerRegistry registry) {
|
public void addViewControllers(@NotNull ViewControllerRegistry registry) {
|
||||||
@@ -26,4 +31,17 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
|
//WebMvcConfigurer.super.addInterceptors(registry);
|
||||||
|
//registry.addInterceptor(jwtInterceptor)
|
||||||
|
//.pathMatcher(new AntPathMatcher())
|
||||||
|
//.addPathPatterns("/**")
|
||||||
|
//.excludePathPatterns("/api/signin")
|
||||||
|
;
|
||||||
|
//registry.addInterceptor(new SampleInterceptor())
|
||||||
|
// .addPathPatterns("/hello/**")
|
||||||
|
//.pathMatcher(new AntPathMatcher())
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ rootProject.name = "spring-boot-examples"
|
|||||||
include(
|
include(
|
||||||
"file-upload", "security", "security-with-jpa", "validation", "testing",
|
"file-upload", "security", "security-with-jpa", "validation", "testing",
|
||||||
"mqtt", "websocket", "restful", "swing", "rest-doc",
|
"mqtt", "websocket", "restful", "swing", "rest-doc",
|
||||||
"cache", "security-with-jwt"
|
"cache", "security-with-jwt", "exception"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user