본문 바로가기
개발/Security

[SpringBoot] Security 로그인 인증, 인가(8) - Controller

by 코딩하는 흰둥이 2023. 4. 30.
반응형

이전글 https://greed-yb.tistory.com/229

 

[SpringBoot] Security 로그인 인증, 인가(7) - html

이전글 https://greed-yb.tistory.com/228 [SpringBoot] Security 로그인 인증, 인가(6) - Service 이전글 https://greed-yb.tistory.com/227 [SpringBoot] Security 로그인 인증, 인가(5) - Vo , Mapper 이전글 https://greed-yb.tistory.com/226 [Sp

greed-yb.tistory.com

 

 

UserController
package com.example.practice.controller.login;

import com.example.practice.service.user.UserService;
import com.example.practice.vo.UserVo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    // 메인 페이지
    @GetMapping("/")
    public String home(Model model) throws Exception{

        //AuthProvider 에서 로그인한 유저의 정보를 받아 옴
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        UserVo userVo = userService.getUserById(username);
        model.addAttribute("user", userVo);
        return "home";
    }

    // login
    @GetMapping("/login")
    public String loginPage() throws Exception{
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 로그인 정보가 없으면 loginPage 로 있으면 / 메인페이지로
        if (authentication instanceof AnonymousAuthenticationToken)
            return "loginPage";
        return "redirect:/";
    }

    // 회원가입 페이지
    @GetMapping("/signup")
    public String signupPage() throws Exception{
        return "signupPage";
    }

    // 회원가입 페이지
    @PostMapping("/signup")
    public String signup(UserVo userVo) throws Exception{
        try {
            userService.signup(userVo);
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/signup";
        }
        return "redirect:/login";
    }

    // 회원 정보 수정
    @GetMapping("/update")
    public String editPage(Model model) throws Exception{
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserVo userVo = userService.getUserById(username);
        model.addAttribute("user", userVo);
        return "editPage";
    }

    // 회원 정보 수정
    @PostMapping("/update")
    public String edit(UserVo userVo) throws Exception{
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        userVo.setId(username);
        userService.edit(userVo);
        return "redirect:/";
    }

    // 권한 인증 실패
    @GetMapping("/denied")
    public String denied(Model model){
        // 보여주고 싶은 메세지를 보내도 되고
        // 임의의 값을 보내 해당 페이지 접속 시 alert 으로 팝업을 띄워줘도 좋다
        String exception = "권한이 없습니다";
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String id = (String)authentication.getPrincipal();
        model.addAttribute("id",id);
        model.addAttribute("exception",exception);
        return "denied";
    }

    // 권한 테스트용 임시 메서드
    @GetMapping("/board/create")
    public String boardCreate() throws Exception {
        return "BoardCreate";
    }

}

댓글