-
Notifications
You must be signed in to change notification settings - Fork 0
Spring Security를 이용하여 로그인/로그아웃 기능 추가 #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EunJuOh33
wants to merge
5
commits into
dev
Choose a base branch
from
feat/security
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e5807aa
[#23] env : pom.xml에 security 파일 추가
EunJuOh33 711f1c9
[#23] env : SecurityConfig 자바 설정. xml 설정 연습필요
EunJuOh33 5dbd8af
[#23] env : 지우고 다시 수정
EunJuOh33 cc42550
[#23] env/front : 로그인 페이지뷰 작성, member나 admin 페이지 접근 시 로그인 페이지로 이동
EunJuOh33 677b60b
[#23] feat/env : 로그인 성공시 - 사용자의 권한에 따라 페이지 이동 처리 설정
EunJuOh33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.zerock.config; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| 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; | ||
| import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
| import org.zerock.security.CustomLoginSuccessHandler; | ||
|
|
||
| import lombok.extern.log4j.Log4j; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| @Log4j | ||
| public class SecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
|
||
| @Override | ||
| public void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
| log.info("configure...................."); | ||
| auth.inMemoryAuthentication().withUser("admin").password("{noop}admin").roles("ADMIN"); | ||
| auth.inMemoryAuthentication().withUser("member").password("{noop}member").roles("MEMBER"); | ||
| } | ||
|
|
||
| @Bean | ||
| public AuthenticationSuccessHandler loginSuccessHandler() { | ||
| return new CustomLoginSuccessHandler(); | ||
| } | ||
|
|
||
| @Override | ||
| public void configure(HttpSecurity http) throws Exception { | ||
|
|
||
| http.authorizeRequests() | ||
| .antMatchers("/samplesecurity/all").permitAll() | ||
| .antMatchers("/samplesecurity/admin").access("hasRole('ROLE_ADMIN')") | ||
| .antMatchers("/samplesecurity/member").access("hasRole('ROLE_MEMBER')"); | ||
|
|
||
| http.formLogin().loginPage("/customLogin").loginProcessingUrl("/login").successHandler(loginSuccessHandler()); | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.zerock.config; | ||
|
|
||
| import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; | ||
|
|
||
| public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer { | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/org/zerock/controller/SecurityCommonController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.zerock.controller; | ||
|
|
||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
||
| import lombok.extern.log4j.Log4j; | ||
|
|
||
| @Controller | ||
| @Log4j | ||
| public class SecurityCommonController { | ||
|
|
||
| @GetMapping("/accessError") | ||
| public void accessDenied(Authentication auth, Model model) { | ||
| log.info("access Denied : " + auth); | ||
|
|
||
| model.addAttribute("msg", "Access Denied"); | ||
| } | ||
|
|
||
| @GetMapping("/customLogin") | ||
| public void loginInput(String error, String logout, Model model) { | ||
|
|
||
| log.info("error: " + error); | ||
| log.info("logout: " + logout); | ||
|
|
||
| if(error != null) { | ||
| model.addAttribute("error", "Login Error Check Your Account"); | ||
| } | ||
|
|
||
| if(logout != null) { | ||
| model.addAttribute("logout", "Logout!!"); | ||
| } | ||
| } | ||
|
|
||
| } |
31 changes: 31 additions & 0 deletions
31
src/main/java/org/zerock/controller/SecuritySampleController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package org.zerock.controller; | ||
|
|
||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
|
|
||
| import lombok.extern.log4j.Log4j; | ||
|
|
||
| @Controller | ||
| @RequestMapping("/samplesecurity/*") | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sample 경로로 연습한 적이 있어서 이번에는 samplesecurity경로를 만들었다.. |
||
| @Log4j | ||
| public class SecuritySampleController { | ||
|
|
||
| /* Spring Security 연습 */ | ||
|
|
||
| @GetMapping("/all") | ||
| public void doAll() { | ||
| log.info("do all can access everybody"); | ||
| } | ||
|
|
||
| @GetMapping("/member") | ||
| public void doMember() { | ||
| log.info("logined member"); | ||
| } | ||
|
|
||
| @GetMapping("/admin") | ||
| public void doAdmin() { | ||
| log.info("admin only"); | ||
| } | ||
|
|
||
| } | ||
47 changes: 47 additions & 0 deletions
47
src/main/java/org/zerock/security/CustomLoginSuccessHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.zerock.security; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
|
|
||
| import lombok.extern.log4j.Log4j; | ||
|
|
||
| @Log4j | ||
| public class CustomLoginSuccessHandler implements AuthenticationSuccessHandler { | ||
|
|
||
| @Override | ||
| public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException { | ||
|
|
||
| log.warn("Login Success"); | ||
|
|
||
| List<String> roleNames = new ArrayList<>(); | ||
|
|
||
| auth.getAuthorities().forEach(authority -> { | ||
| roleNames.add(authority.getAuthority()); | ||
| }); | ||
|
|
||
| log.warn("ROLE NAMES: " + roleNames); | ||
|
|
||
| /* 사용자가 가진 모든 권한을 문자열로 체크 */ | ||
|
|
||
| if(roleNames.contains("ROLE_ADMIN")) { // "ROLE_ADMIN" 권한을 가졌다면 로그인 후 바로 /samplesecurity/admin 페이지로 이동 | ||
| response.sendRedirect("/samplesecurity/admin"); | ||
| return; | ||
| } | ||
|
|
||
| if(roleNames.contains("ROLE_MEMBER")) { | ||
| response.sendRedirect("/samplesecurity/member"); | ||
| return; | ||
| } | ||
|
|
||
| response.sendRedirect("/"); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <%@ page language="java" contentType="text/html; charset=UTF-8" | ||
| pageEncoding="UTF-8"%> | ||
|
|
||
| <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | ||
| <%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %> | ||
| <%@ page import="java.util.*" %> | ||
|
|
||
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
|
|
||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| <title>Insert title here</title> | ||
| </head> | ||
| <body> | ||
| <h1>Access Denied Page</h1> | ||
|
|
||
| <h2><c:out value="${SPRING_SECURITY_403_EXCEPTION.getMessage() }" /></h2> | ||
|
|
||
| <h2><c:out value="${msg }" /></h2> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <%@ page language="java" contentType="text/html; charset=UTF-8" | ||
| pageEncoding="UTF-8"%> | ||
|
|
||
| <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> | ||
|
|
||
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
|
|
||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| <title>Insert title here</title> | ||
| </head> | ||
| <body> | ||
|
|
||
| <h1>Custom Login Page</h1> | ||
| <h2><c:out value="${error }" /></h2> | ||
| <h2><c:out value="${logout }" /></h2> | ||
|
|
||
| <form method='post' action="/login"> | ||
|
|
||
| <div> | ||
| <input type='text' name='username' value='admin'> | ||
| </div> | ||
| <div> | ||
| <input type='password' name='password' value='admin'> | ||
| </div> | ||
| <div> | ||
| <input type='submit'> | ||
| </div> | ||
| <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /> | ||
|
|
||
| </form> | ||
|
|
||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <%@ page language="java" contentType="text/html; charset=UTF-8" | ||
| pageEncoding="UTF-8"%> | ||
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| <title>Insert title here</title> | ||
| </head> | ||
| <body> | ||
| <h1>/sample/admin page</h1> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <%@ page language="java" contentType="text/html; charset=UTF-8" | ||
| pageEncoding="UTF-8"%> | ||
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
|
|
||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| <title>Insert title here</title> | ||
| </head> | ||
| <body> | ||
| <h1>/sample/all page</h1> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <%@ page language="java" contentType="text/html; charset=UTF-8" | ||
| pageEncoding="UTF-8"%> | ||
| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
|
|
||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
| <title>Insert title here</title> | ||
| </head> | ||
| <body> | ||
| <h1>/sample/member page</h1> | ||
| </body> | ||
| </html> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
target/m2e-wtp/web-resources/META-INF/maven/org.zerock/controller/pom.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추가