본문 바로가기
Spring/Spring 기초

e7e샘의 시큐리티 설정4(메뉴얼 로그인)

by 미눅스[멘토] 2023. 8. 31.
728x90

 

package com.minu.sec.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/oho")
public class OhoController {

	@GetMapping("/sukil")
	public String getSukil() {
		return "home";
	}
	
	@GetMapping("/suji")
	public String getSuji() {
		return "sectag";
	}
	
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/security/tags"  prefix="sec"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>principal이 뭐에 대응하는지만 알면 Good</h1>
<h2>principal은 CustomUser에 대응함(해당한다)!</h2>
<p><sec:authentication property="principal"/></p>
<p><sec:authentication property="principal.member"/></p>
<p><sec:authentication property="principal.member.username"/></p>
<p><sec:authentication property="principal.username"   var="sukil"/></p>
<p><sec:authentication property="principal.member.authList"  var="myAuths"/></p>
<h1>출력${sukil }</h1>
<h1>${myAuths[0].userid}</h1>
<h1>${myAuths[0].auth}</h1>
</body>
</html>

 

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>오늘 더 멋진 석일</title>
</head>
<body>
	<h1>구두가 뽀인토</h1>
	<form action="/sec/slogin" method="post">
		<sec:csrfInput/> <!-- 요건 서버가 해석하는 서버 태그 곧 jstl은 서버태그 -->
		아이딩<input type="text" name="username" value=""><br/>
		암호<input type="password" name="password" value=""><br/>
		<button type="submit">로그잉</button>
	</form>
	
	<h1>manual login 어쩌다 필요</h1>
	<form action="/sec/mylogin" method="post">
		<sec:csrfInput/> <!-- 요건 서버가 해석하는 서버 태그 곧 jstl은 서버태그 -->
		아이딩2<input type="text" name="myname" value=""><br/>
		암호2<input type="password" name="mypass" value=""><br/>
		<button type="submit">로그잉2</button>
	</form>
</body>
</html>

 

 

package com.minu.sec.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
public class MyLoginController {

	@Autowired
	private AuthenticationManager authMgr;
	
	@PostMapping("/mylogin")
	public String MyLogin(HttpServletRequest req) {
		//Autowired 잘 되었는지? 확인(D.I)
		log.debug("check:"+authMgr);
		
		//DB에 존재하는 아이디/암호여야 함!, 안 그럼 없는 사용자! 
		UsernamePasswordAuthenticationToken myAuth =
				new UsernamePasswordAuthenticationToken("jinsu3", "jinsu3");

		//인증 매니져가 토큰을 인증하고, 인증정보를 만듬
		Authentication auth = authMgr.authenticate(myAuth);
		
		
		SecurityContext sc = SecurityContextHolder.getContext();
		//인증 정보를 등록
		sc.setAuthentication(auth);
		
		//Session이 없으면 생성
		HttpSession session = req.getSession(true);  
		//Session에 시큐리티 컨텍스트 등록
		//org.springframework.security.web.context.HttpSessionSecurityContextRepository.class에 
		// "SPRING_SECURITY_CONTEXT_KEY" 이 정의 되어 있음. 그냥 참고망
		session.setAttribute("SPRING_SECURITY_CONTEXT_KEY", sc);
		
		return "home";
	}
}