Skip to content
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

[20210708] (Spring Security) @EnableGlobalMethodSecurity vs @EnableWebSecurity, @PreAuthorize, @PostAuthorize #170

Open
JuHyun419 opened this issue Jul 8, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

JuHyun419 commented Jul 8, 2021

Spring Security - @EnableGlobalMethodSecurity vs @EnableWebSecurity

@EnableGlobalMethodSecurity

@EnableWebSeucirty

  • HttpSecurity을 통한 설정을 제공하는 스프링 시큐리티
메서드 단계에서 시큐리티를 적용하는 @PreAuthorize, @PostAuthorize 와 같은 어노테이션을 사용한다면
@EnableGlobalMethodSecurity 설정을, 그게 아닌 일반적인 경우는 @EnableWebSecurity 을 선언


@Configuration
@Log4j2
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    ...

@PreAuthorize

  • method에 진입하기 전 권한 체크

@PostAuthorize

  • method 실행 후 권한 체크
    // 관리자(admin) 권한을 가진 사용자만 접근 가능
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin")
    public void admin() {
        log.info("admin........");
    }


    // 특별 정해진 사용자만 해당 메서드를 실행하도록 설정 => "user95@zerock.org" 의 사용자만 해당 메소드 접근 가능
    @PreAuthorize("#clubAuthMemberDto != null && #clubAuthMemberDto.username eq \"user95@zerock.org\"")
    @GetMapping("/only")
    public String memberOnly(@AuthenticationPrincipal ClubAuthMemberDto clubAuthMemberDto) {
        log.info(clubAuthMemberDto);

        return "/sample/admin";
    }


    @PostAuthorize
      ("returnObject.username == authentication.principal.nickName")
    public CustomUser loadUserDetail(String username) {
        return userRoleRepository.loadUserByUserName(username);
    }
@JuHyun419 JuHyun419 changed the title [20210708] (Spring Security) @EnableGlobalMethodSecurity vs @EnableWebSecurity [20210708] (Spring Security) @EnableGlobalMethodSecurity vs @EnableWebSecurity, @PreAuthorize, @PostAuthorize Jul 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant