Spring Security-WebSecurityConfigurerAdapter

10962 단어 Spring-Boot
인증
  • 인증 - Authentication 은 통신 의 상대방 이 누구 인지 확인 합 니 다. 사용자 이름, 비밀번호, Token, 인증번호 등 여러 가지 방식 을 통 해 – 보안 만 허용 하고 방 에 들 어간 후에 방 장 치 를 조작 할 수 있 는 어떠한 권한 도 없습니다. 주로 세 가지 방식
  • 이 있 습 니 다.
  • WHAT YOU ARE (inherence factor) 는 소리, 외모 등 을 통 해 상대방 을 직접 확인 할 수 있다.
  • WHAT YOU HAVE (holding factor) 신분증 등 각종 증명 정 보 를 통 해 상대방 확인
  • WHAT YOU KNOW (knowledge factor) 는 상대방 만 아 는 비밀번호, Token, 인증번호 등 비밀 정 보 를 통 해 상대방 을 확인한다
  • SpringBoot 의 간단 한 인증 실현, Spring Security 5 - 인증 인증 참조 가능
  • 승인 - Authorization 권한 수여, 통신 상대방 에 게 일정한 권한 부여
  • AuthenticationManagerBuilder
    WebSecurity ConfigurerAdapter 에서 proctected void configure (AuthenticationManager Builder auth) 를 재 작성 하여 Inmemory, LDAP, JDBC 방식 의 인증 을 실현 하 는 동시에 SpringBoot 는 기본 Login 인터페이스 (DefaultLoginPageConfigurer < > () 를 제공 합 니 다.
    HttpSecurity
    WebSecurity ConfigurerAdapter 에서 protected void configure (HttpSecurity http) 재 작성 을 통 해 CORS 를 구현 하고 로그 인 / 로그아웃 인터페이스 를 사용자 정의 합 니 다. 인증 등 사용자 정의 설정 기능 이 기본 으로 지정 되 어 있 는 지 여부 입 니 다.
    http
    	.authorizeRequests().anyRequest().authenticated() //   request     
    	.and()
    	.formLogin()   //   login    
    	.and()
    	.httpBasic();  // Basic  
    

    CORS
    영문 전 칭;CORS (Cross - Origin Resource Sharing) 는 원본 자원 을 넘 어 공유 하고 신뢰 하 는 웹 사이트 에 대한 크로스 도 메 인 접근 권한 을 제공 합 니 다. 어떤 크로스 도 메 인 요청 이 Spring Security 5 의 기본 적 인 유효 CORS 를 유연 하 게 지정 하여 안전 한 외부 방문 허용 을 실현 할 수 있 습 니 다.참조Security-CORS
    Spring MVC 에서 도 CORS 의 크로스 도 메 인 설정 참조 가 제공 되 었 습 니 다.MVC-CORS
    CSRF
    참조SpringSecurity-CSRF
    Login / Logout
    자신의 로그 인 인터페이스 와 로그 인 성공 후의 점프 페이지 를 사용자 정의 합 니 다.참조FreeMaker 또는 Thymeleaf 2. X 시 리 즈 를 사용 하면 절차;
  • 의존 추가
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    
  • 프로필 (application. properties) 에 추가
    spring.thymeleaf.prefix: classpath:/templates/
    
  • 간단 한 html 파일 생 성 (src / main / resources / templates / test. html)
    
    <html lang="en">
    <head>
     <meta charset="UTF-8">
     <title>Titletitle>
     <script src="https://cdn.bootcss.com/jquery/2.1.0/jquery.min.js">script>
     <script type="text/javascript">
      function crosRequest(){
         $.ajax({
           url:'http://localhost:1234/sendmessage',
           type:'get',
           dataType:'json',
           success:function(data){
           console.log(data);
          }
         });
      }
     script>
    head>
    <body>
     <button onclick="crosRequest()">crosRequestbutton>
    body>
    html>
    
  • Controller 의 생 성
    @Controller
    public class sampleController {
        @RequestMapping("/hello")
        public String getHello(HashMap<String, Object> map) {
            map.put("hello", "Welcom Page.");
            return "/test";
        }
    }
    
  • 테스트 를 실행 하면 됩 니 다
       http://localhost:xxxx/hello
    
  • WebSecurity
    WebSecurity ConfigurerAdapter 에서 proctected void configure (WebSecurity http) 를 다시 써 서 지정 한 경로 의 접근 제어 기본 빈 장 치 를 실현 합 니 다.
    @Override
        public void configure(WebSecurity http) throws Exception {
            // Remove monitoring path from SpringSecurity。
            http.ignoring().antMatchers("/api/**");
        }
    

    좋은 웹페이지 즐겨찾기