티스토리 뷰

Dev/Spring

[security] 1. 기본 설정

Alxndr 2021. 5. 11. 11:12

Spring Security Dependency

  • build.gradle
dependencies { implementation 'org.springframework.boot:spring-boot-starter-security' }
  1. 인증 가능
  2. 사용자 정보 알 수 있다.

Security Setting

  • SecurityConfig.java
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()    // 인가
                    .mvcMatchers("/", "/info").permitAll()  // 1
                    .mvcMatchers("/admin").hasRole("ADMIN") // 2
                    .anyRequest().authenticated();  //3

        http.formLogin()    // 4
                .and()
                .httpBasic();   // 5
    }
}
  1. /, /info 접근은 권한이 없어도 접근 가능
  2. ADMIN ROLE 을 가진 사용자만 접근 가능
  3. 그 외의 모든 접근은 인증 필요
  4. formLogin, httpBasic 인증을 사용

Sample Controller

  • SampleController.java
@Controller
public class SampleController {

    @GetMapping("/")
    public String index(Model model, Principal principal) {
        if (principal == null) {
            model.addAttribute("message", "Hello Spring Security");
        } else {
            model.addAttribute("message", "Hello " + principal.getName());
        }

        return "index";
    }

    @GetMapping("/info")
    public String info(Model model) {
        model.addAttribute("message", "Hello Spring Security");

        return "info";
    }

    @GetMapping("/dashboard")
    public String dashboard(Model model, Principal principal) {
        model.addAttribute("message", "Hello from Dashboard, " + principal.getName());

        return "dashboard";
    }

    @GetMapping("/admin")
    public String admin(Model model, Principal principal) {
        model.addAttribute("message", "Hello Admin, " + principal.getName());

        return "admin";
    }
}

서버 구동 후 /dashboard로 접근하려 하면 Spring Security의 기본 Login화면이 나오게 됩니다.

  • 기본 계정
    • ID: user
    • PW: 로그에 나오는 Using generated security password : ~~~~ 

위의 정보로 로그인하면 접근 할 수 있습니다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함