SpringSecurity 권한 제어 실현 원리 분석

메뉴 제어:


이 사용자 가 이런 캐릭터 가 있 는 지 없 는 지 판단 할 수 있 습 니 다.없 으 면 보 여주 지 않 습 니 다.


데이터 제어:
데 이 터 는 모두 백 엔 드 에서 찾 았 기 때문에 백 엔 드 에서 권한 을 제어 하면 됩 니 다.

<!--
              
    jsr250-annotations="enabled"    jsr250-api   ,  jsr250-api jar 
    pre-post-annotations="enabled"    spring     
    secured-annotations="enabled"   SpringSecurity     
   -->
  <security:global-method-security jsr250-annotations="enabled"
                   pre-post-annotations="enabled"
                   secured-annotations="enabled"/>
주:이것 은 뮤 직 비디오 c 용기 에 넣 어야 합 니 다.하위 용 기 는 주 용기 에 접근 할 수 있 기 때문에 주 용기 가 하위 용기 에 접근 할 수 없습니다.

/             ROLE_ADMIN  ROLE_PRODUCT    
@Controller
@RequestMapping("/product")
@RolesAllowed({"ROLE_ADMIN","ROLE_PRODUCT"})//JSR-250  
public class ProductController {
@RequestMapping("/findAll")
public String findAll(){
return "product-list";
}
}
//      findAll    ROLE_ADMIN  ROLE_PRODUCT    
@Controller
@RequestMapping("/product")
public class ProductController {
@RequestMapping("/findAll")
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")//spring     
public String findAll(){
return "product-list";
}
}
//             ROLE_ADMIN  ROLE_PRODUCT    
@Controller
@RequestMapping("/product")
@Secured({"ROLE_ADMIN","ROLE_PRODUCT"})//SpringSecurity  
public class ProductController {
@RequestMapping("/findAll")
public String findAll(){
return "product-list";
}
}
근 데 403 번 보고 할 거 예요.방문 이 안 돼 요.
방식 1:spring-security.xml 설정 파일 에서 처리

<!--     spring el     Spring Security           (   )-->
<security:http auto-config="true" use-expressions="true">
<!--      -->
<!--403    -->
<security:access-denied-handler error-page="/403.jsp"/>
</security:http>
방식 2:웹.xml 에서 처리

<error-page>
  <error-code>403</error-code>
  <location>/403.jsp</location>
</error-page>
방식 3:이상 프로세서 작성

/**
 * @author WGR
 * @create 2020/3/2 -- 17:33
 */
@ControllerAdvice
public class ControllerExceptionAdvice {

  //    AccessDeniedException     403.jsp  
  @ExceptionHandler(AccessDeniedException.class)
  public String exceptionAdvice(){
    System.out.println("1234");
    return "forward:/403.jsp";
  }
}
주:spring 프로젝트 라면 이것 도 스 캔 해 야 합 니 다.


이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기