spring登陸驗證注解區分登陸角色

jopen 9年前發布 | 27K 次閱讀 Spring JEE框架

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;     }

}</pre>

這個是我的注解

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; }</pre>

這個是枚舉

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;     } }</pre>

注解用法

    @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>


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!