【SpringSecurity】授权操作,处理权限不足异常

【SpringSecurity】授权操作,处理权限不足异常

  • 1. 授权操作
    • 1.1 开启授权的注解支持
    • 1.2 在注解支持对应类或者方法上添加注解
  • 2. 处理权限不足异常
    • 2.1 方式一:在spring-security.xml配置文件中处理
    • 2.2 方式二:在web.xml中处理
    • 2.3 方式三:编写异常处理器

1. 授权操作

SpringSecurity可以通过注解的方式来控制类或者方法的访问权限。注解需要对应的注解支持,若注解放在controller类中,对应注解支持应该放在mvc配置文件中,因为controller类是有mvc配置文件扫描并创建的,同理,注解放在service类中,对应注解支持应该放在spring配置文件中。本篇博文就只将注解放在了controller中了。

1.1 开启授权的注解支持

这里给大家演示三类注解,但实际开发中,用一类即可!
spring-mvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:security="http://www.springframework.org/schema/security"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
			    http://www.springframework.org/schema/beans/spring-beans.xsd
			    http://www.springframework.org/schema/context
			    http://www.springframework.org/schema/context/spring-context.xsd
			    http://www.springframework.org/schema/aop
			    http://www.springframework.org/schema/aop/spring-aop.xsd
			    http://www.springframework.org/schema/tx
			    http://www.springframework.org/schema/tx/spring-tx.xsd
			    http://www.springframework.org/schema/mvc
			    http://www.springframework.org/schema/mvc/spring-mvc.xsd
			    http://www.springframework.org/schema/security
			    http://www.springframework.org/schema/security/spring-security.xsd">

    <context:component-scan base-package="com.siyi.controller"/>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    bean>
    <mvc:default-servlet-handler/>

    
    <security:global-method-security secured-annotations="enabled"
                                     pre-post-annotations="enabled"
                                     jsr250-annotations="enabled"/>
beans>

1.2 在注解支持对应类或者方法上添加注解

package com.siyi.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/product")
public class ProductController {

//表示当前类中所有方法都需要ROLE_ADMIN或者ROLE_PRODUCT才能访问
//    @Secured({"ROLE_PRODUCT","ROLE_ADMIN"})//springsecurity内部制定的注解
//    @RolesAllowed({"ROLE_PRODUCT","ROLE_ADMIN"})//jsr250注解
    @PreAuthorize("hasAnyAuthority('ROLE_PRODUCT','ROLE_ADMIN')")//spring的el表达式注解
    @RequestMapping("/findAll")
    public String findAll(){
        return "product-list";
    }
}

这样设置后,如果当前用户没有这个方法要求的角色,那么就会抛出权限不足异常,无法操作。

2. 处理权限不足异常

【SpringSecurity】授权操作,处理权限不足异常_第1张图片
每次出现权限不足时,默认出现的是这个页面,对于我们来说看这个大家都明白是什么意思,但是对于客户来说就很不友好,所以我们要将页面设置成我们自己的403页面。

2.1 方式一:在spring-security.xml配置文件中处理


	<security:http auto-config="true" use-expressions="true">
	
	
	<security:access-denied-handler error-page="/403.jsp"/>
security:http>

但是这种方式只能设置403,而我们在实际中容易出现403,404,500等,所以这种方式太局限了。

2.2 方式二:在web.xml中处理

    
    <error-page>
        <error-code>403error-code>
        <location>/403.jsplocation>
    error-page>
    <error-page>
        <error-code>404error-code>
        <location>/404.jsplocation>
    error-page>

2.3 方式三:编写异常处理器

package com.siyi.advice;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class HandlerControllerAdvice {

    @ExceptionHandler({AccessDeniedException.class})
    public String handlerException(){
        return "redirect:/403.jsp";
    }

    @ExceptionHandler(RuntimeException.class)
    public String runtimeHandlerException(){
        return "redirect:/500.jsp";
    }
}

你可能感兴趣的:(SpringSecurity,SpringSecurity,授权操作,处理权限不足异常)