Compare commits
5 Commits
feature/bl
...
bugfix/loc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
541320065c | ||
|
|
bc5e033ee7 | ||
|
|
d51ed655cc | ||
|
|
a6a65b7250 | ||
|
|
9ee074ab57 |
10
README.md
10
README.md
@@ -1,3 +1,8 @@
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
# Spring Rest Api 만들기 프로젝트
|
||||
|
||||
### 0. 개요
|
||||
@@ -120,3 +125,8 @@ alter table user_roles
|
||||
- https://daddyprogrammer.org/post/3870/spring-rest-api-redis-caching/
|
||||
- Git
|
||||
- https://github.com/codej99/SpringRestApi/tree/cache-data-redis
|
||||
- SpringBoot2로 Rest api 만들기(16) – AOP와 Custom Annotation을 이용한 금칙어(Forbidden Word) 처리
|
||||
- Document
|
||||
- https://daddyprogrammer.org/post/11356/springboot2-forbidden-word-by-aop-annotation/
|
||||
- Git
|
||||
- https://github.com/codej99/SpringRestApi/tree/feature/block_fobidden_word
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.rest.api.config.security;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.web.access.AccessDeniedHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
@@ -14,7 +17,9 @@ import java.io.IOException;
|
||||
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception) throws IOException {
|
||||
response.sendRedirect("/exception/accessdenied");
|
||||
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception) throws IOException, ServletException {
|
||||
response.setLocale(LocaleContextHolder.getLocale());
|
||||
RequestDispatcher rd = request.getRequestDispatcher("/exception/accessdenied");
|
||||
rd.forward(request, response);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.rest.api.config.security;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
@@ -13,7 +16,9 @@ import java.io.IOException;
|
||||
@Component
|
||||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
|
||||
@Override
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException {
|
||||
response.sendRedirect("/exception/entrypoint");
|
||||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException, ServletException {
|
||||
response.setLocale(LocaleContextHolder.getLocale());
|
||||
RequestDispatcher rd = request.getRequestDispatcher("/exception/entrypoint");
|
||||
rd.forward(request, response);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.rest.api.config.security;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
@@ -10,6 +11,12 @@ import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Configuration
|
||||
@@ -39,6 +46,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
.and()
|
||||
.exceptionHandling().authenticationEntryPoint(new CustomAuthenticationEntryPoint())
|
||||
.and()
|
||||
.addFilterBefore(new AthenticationEntryLocaleFilter(), UsernamePasswordAuthenticationFilter.class)
|
||||
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class); // jwt token 필터를 id/password 인증 필터 전에 넣어라.
|
||||
|
||||
}
|
||||
@@ -49,4 +57,31 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
"/swagger-ui.html", "/webjars/**", "/swagger/**");
|
||||
|
||||
}
|
||||
|
||||
private static class AthenticationEntryLocaleFilter implements Filter {
|
||||
private SessionLocaleResolver localeResolver;
|
||||
|
||||
private AthenticationEntryLocaleFilter() {
|
||||
localeResolver = new SessionLocaleResolver();
|
||||
localeResolver.setDefaultLocale(Locale.KOREAN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||
if(request.getParameter("lang") != null)
|
||||
localeResolver.setDefaultLocale(Locale.forLanguageTag(request.getParameter("lang")));
|
||||
Locale locale = localeResolver.resolveLocale((HttpServletRequest) request);
|
||||
LocaleContextHolder.setLocale(locale);
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public class ExceptionController {
|
||||
throw new CAuthenticationEntryPointException();
|
||||
}
|
||||
|
||||
@GetMapping(value = "/accessdenied")
|
||||
@RequestMapping(value = "/accessdenied")
|
||||
public CommonResult accessdeniedException() {
|
||||
throw new AccessDeniedException("");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
logging:
|
||||
level:
|
||||
root: debug
|
||||
root: info
|
||||
com.rest.api: debug
|
||||
|
||||
spring:
|
||||
|
||||
Reference in New Issue
Block a user