spring登陆验证注解区分登陆角色

先上拦截器代码

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.tutor.normal.entity.TutorUser;
import com.tutor.normal.service.ITutorUserService;
import com.tutor.normal.status.UserType;
import com.tutor.tool.util.ResultUtil;

public class TutorLoginFilter extends HandlerInterceptorAdapter {
    private static Logger log = LoggerFactory.getLogger(TutorLoginFilter.class);
    
    @Resource(name = "tutorUserService")
    private ITutorUserService tutorUserService;
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info(handler.getClass().toString());
        
        String token = request.getParameter("token");
        
        TutorUser user = null;
        try {
            HandlerMethod method = (HandlerMethod) handler;
            TutorLoginAnnotation annotation = method.getMethodAnnotation(TutorLoginAnnotation.class);
            
            if(annotation == null) {
                return true;
            } else {
                user = tutorUserService.validUser(token, annotation.type().getValue());
                log.info("用户user=" + user);
            }
            
        } catch (Exception e) {
            log.error(e.getMessage());
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(ResultUtil.returnJson(e));
            response.getWriter().flush();
            response.getWriter().close();
            return false;
        }
        return true;
    }

}

这个是我的注解

package com.tutor.normal.filter;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.tutor.normal.status.UserType;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface TutorLoginAnnotation {
    public UserType type() default UserType.ALL;
}

这个是枚举

package com.tutor.normal.status;

public enum UserType {
    STUDENT("学生", 1), TEACHER("老师", 2), ALL("全部", 0);
    private String name;
    private int value;

    private UserType(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

注解用法

    @TutorLoginAnnotation(type = UserType.TEACHER)
    @RequestMapping("/tea/search/{v}")
    @ResponseBody
    public ResultBean myfun() {
        ...
    }

最后spring中拦截器的配置

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/stu/**" />
            <mvc:mapping path="/tch/**" />
            <mvc:mapping path="/ord/**" />
            <bean class="com.tutor.normal.filter.TutorLoginFilter" />
        </mvc:interceptor>
    </mvc:interceptors>


转载于:https://my.oschina.net/u/2288185/blog/364442

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值