[CowAPI] 26. Notice

준돌·2022년 7월 12일
0

오늘의 Cow

목록 보기
31/45

1. 목표

  • 공지 기능에 권한 기능 부여하기

2. 코드

// 기존의 JWT에 Role 추가
// 다른 객체에 의해 변경되지 않아야하는 값으로 싱글톤 패턴 사용

SimpleGrantedAuthority role =  (users.getIsAdmin()) ? new SimpleGrantedAuthority("ROLE_ADMIN") : new SimpleGrantedAuthority("ROLE_USER");
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userEmail,userPassword, Collections.singleton(role));

3. Security 사용

// 기존의 Security에 Matcher 추가

.antMatchers(HttpMethod.POST, "/notice").hasRole("ADMIN")
.antMatchers(HttpMethod.PUT, "/notice/**").hasRole("ADMIN")
.antMatchers(HttpMethod.DELETE, "/notice/**").hasRole("ADMIN")
profile
눈 내리는 겨울이 좋아!

0개의 댓글