package com.sptg.gateway.handler;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.exception.BodyAuthenticationException;
import com.sptg.common.security.exception.BodyAuthorizationException;
import com.sptg.common.security.exception.ViewAuthenticationException;
import com.sptg.common.security.exception.ViewAuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * title: SecurityExceptionHandler
 * package: com.sptg.gateway.handler
 * description: 权限异常拦截
 *
 *                  
 *                 
 */
@ControllerAdvice
public class SecurityExceptionHandler {

    /**
     * 功能描述: 未登陆状态返回值
     * @return ResultBean
     *                  
     *                       
     */
    @ResponseBody
    @ExceptionHandler(value = BodyAuthenticationException.class)
    public ResultBean<String> bodyAuthenticationException(){
        return new ResultBean<>(ResultEnum.UNAUTHORIZED);
    }

    /**
     * 功能描述: 未登陆视图
     * @return ModelAndView
     *                  
     *                       
     */
    @ExceptionHandler(value = ViewAuthenticationException.class)
    public ModelAndView viewAuthenticationException(){
        return new ModelAndView("redirect:/permissions/authentication/login");
    }

    /**
     * 功能描述: 无权访问返回值
     * @return ResultBean
     *                  
     *                       
     */
    @ResponseBody
    @ExceptionHandler(value = BodyAuthorizationException.class)
    public ResultBean<String> bodyAuthorizationException(){
        return new ResultBean<>(ResultEnum.FORBIDDEN);
    }

    /**
     * 功能描述: 无权访问页面
     * @param e 无权访问异常信息
     * @return ModelAndView
     *                  
     * date:  2018/10/30 23:18
     */
    @ExceptionHandler(value = ViewAuthorizationException.class)
    public ModelAndView viewAuthorizationException(ViewAuthorizationException e){
        ModelAndView modelAndView = new ModelAndView("permissions/403");
        modelAndView.addObject("msg",e.getMessage());
        return modelAndView;
    }
}
