2021-08-02
This commit is contained in:
20
security/src/main/java/kr/pe/elex/examples/Application.java
Normal file
20
security/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);
|
||||
}
|
||||
|
||||
}
|
||||
37
security/src/main/java/kr/pe/elex/examples/MyController.java
Normal file
37
security/src/main/java/kr/pe/elex/examples/MyController.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class MyController {
|
||||
|
||||
@GetMapping({"/"})
|
||||
public String index() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@GetMapping({"/login"})
|
||||
public String login() {
|
||||
return "login";
|
||||
}
|
||||
|
||||
@GetMapping({"/info"})
|
||||
public String info() {
|
||||
return "normal_info";
|
||||
}
|
||||
|
||||
@GetMapping({"/secure"})
|
||||
public String secure() {
|
||||
return "secure_info";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
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;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(@NotNull HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/", "/info").permitAll() // 아무나 접근 가능
|
||||
//.antMatchers("/admin").hasAnyRole("ADMIN")
|
||||
.anyRequest().authenticated() // 그 외에는 인증해야 접근 가능
|
||||
.and()
|
||||
.formLogin()// 커스텀 로그인 폼
|
||||
.loginPage("/login")
|
||||
.usernameParameter("user_id") // 로그인 폼 매개변수명 지정
|
||||
.passwordParameter("user_pw")
|
||||
.permitAll()
|
||||
.and()
|
||||
.logout()// 로그아웃
|
||||
.logoutSuccessUrl("/")
|
||||
.permitAll()
|
||||
//.and().httpBasic();
|
||||
//.and().csrf().disable()
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void configure(@NotNull AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication()
|
||||
.withUser("user1")
|
||||
.password("{noop}pw1")
|
||||
.authorities("ROLE_USER")
|
||||
.and()
|
||||
.withUser("user2")
|
||||
.password("{noop}pw2")
|
||||
.authorities("ROLE_USER")
|
||||
;
|
||||
}
|
||||
}
|
||||
23
security/src/main/java/kr/pe/elex/examples/WebConfig.java
Normal file
23
security/src/main/java/kr/pe/elex/examples/WebConfig.java
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
/*
|
||||
@Override
|
||||
public void addViewControllers(@NotNull ViewControllerRegistry registry) {
|
||||
registry.addViewController("/login");
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Spring-boot Examples
|
||||
*
|
||||
* Copyright (c) 2021. Elex. All Rights Reserved.
|
||||
* https://www.elex-project.com/
|
||||
*/
|
||||
|
||||
package kr.pe.elex.examples;
|
||||
7
security/src/main/resources/application.yaml
Normal file
7
security/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
application:
|
||||
name: My spring-boot project
|
||||
mustache:
|
||||
expose-request-attributes: true # 뷰에서 CSRF를 뿌려주기 위해서 필요합니다.
|
||||
server:
|
||||
port: 8080
|
||||
10
security/src/main/resources/banner.txt
Normal file
10
security/src/main/resources/banner.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
('-. ('-. ) (`-.
|
||||
_( OO) _( OO) ( OO ).
|
||||
(,------.,--. (,------.(_/. \_)-.
|
||||
| .---'| |.-') | .---' \ `.' /
|
||||
| | | | OO ) | | \ /\
|
||||
(| '--. | |`-' |(| '--. \ \ |
|
||||
| .--'(| '---.' | .--' .' \_)
|
||||
| `---.| | | `---. / .'. \
|
||||
`------'`------' `------''--' '--'
|
||||
powered by ELEX
|
||||
44
security/src/main/resources/logback-spring.xml
Normal file
44
security/src/main/resources/logback-spring.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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>
|
||||
|
||||
<root level="verbose">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="ROLLING-FILE"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
4
security/src/main/resources/templates/home.mustache
Normal file
4
security/src/main/resources/templates/home.mustache
Normal file
@@ -0,0 +1,4 @@
|
||||
<h1>홈</h1>
|
||||
<p>테스트 페이지입니다.</p>
|
||||
|
||||
{{> links}}
|
||||
6
security/src/main/resources/templates/links.mustache
Normal file
6
security/src/main/resources/templates/links.mustache
Normal file
@@ -0,0 +1,6 @@
|
||||
<div>
|
||||
<a href="/">홈</a> |
|
||||
<a href="/info">일반</a> |
|
||||
<a href="/secure">중요</a> |
|
||||
<a href="/login">로그인</a>
|
||||
</div>
|
||||
11
security/src/main/resources/templates/login.mustache
Normal file
11
security/src/main/resources/templates/login.mustache
Normal file
@@ -0,0 +1,11 @@
|
||||
<h1>로그인</h1>
|
||||
<form action="/login" method="POST">
|
||||
<label for="user_id">ID:</label>
|
||||
<input type="text" name="user_id" id="user_id"/>
|
||||
<label for="user_pw">PW:</label>
|
||||
<input type="password" name="user_pw" id="user_pw"/>
|
||||
<input type="hidden" name="_csrf" value="{{_csrf.token}}">
|
||||
<input type="submit" value="로그인"/>
|
||||
</form>
|
||||
|
||||
{{> links}}
|
||||
@@ -0,0 +1,3 @@
|
||||
<h1>아무나 볼 수 있는 페이지</h1>
|
||||
<p>This page doesn't contain any Important messages.</p>
|
||||
{{> links}}
|
||||
@@ -0,0 +1,8 @@
|
||||
<h1>로그인해야 볼 수 있는 페이지</h1>
|
||||
<p>This page contains a Important message.</p>
|
||||
<form action="/logout" method="POST">
|
||||
{{! CSRF 토큰이 없으면 에러 남. }}
|
||||
<input type="hidden" name="_csrf" value="{{_csrf.token}}">
|
||||
<button type="submit">로그아웃</button>
|
||||
</form>
|
||||
{{> links}}
|
||||
Reference in New Issue
Block a user