spring 拦截器取不到session里面的值???

dengguikun 发布于 2017/05/24 15:38
阅读 388
收藏 1
加载中
0
阿信sxq
阿信sxq

能的吧

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import cn.songxinqiang.eg.exception.AuthenticationException;
import cn.songxinqiang.eg.model.User;
import cn.songxinqiang.eg.model.config.UserRole;

/**
 * 检查静态资源访问的权限,对于不具有权限的请求抛出{@linkplain AuthenticationException}异常
 *
 * @author 阿信sxq
 *
 */
public class StaticResourceFilter extends OncePerRequestFilter {

    private UserRole role;

    /**
     * {@inheritDoc}<br>
     *
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {
        Object obj = request.getSession().getAttribute(“SESSION_USER”);
        if (obj instanceof User) {
            User user = (User) obj;
            if (!user.getRole().equals(role)) {
                throw new AuthenticationException();
            } else {
                filterChain.doFilter(request, response);
            }
        } else {
            throw new AuthenticationException();
        }
    }

    public void setRole(UserRole role) {
        this.role = role;
    }

}

 

0
dengguikun
dengguikun

前面请求的时候一直为空,获取不到数据,结果发现是junit测试的原因

返回顶部
顶部