package org.onetwo.boot.core.jwt;

import java.util.Optional;

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

import org.apache.commons.lang3.StringUtils;
import org.onetwo.boot.core.web.mvc.interceptor.MvcInterceptorAdapter;
import org.onetwo.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;

/**
 * @author wayshall
 * <br/>
 */
public class JwtMvcInterceptor extends MvcInterceptorAdapter {
	private String authHeaderName = JwtUtils.DEFAULT_HEADER_KEY;
	@Autowired
	private JwtTokenService jwtTokenService;
	private boolean canBeAnonymous;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handler) {
		String token = request.getHeader(authHeaderName);
		if(StringUtils.isBlank(token) && canBeAnonymous){
			return true;
		}
		Optional<JwtUserDetail> userOpt = Optional.empty();
		try {
			userOpt = JwtUtils.getOrSetJwtUserDetail(request, jwtTokenService, authHeaderName);
		} catch (ServiceException e) {
			/*if(e.getExceptionType() instanceof JwtErrors && e.getExceptionType()!=JwtErrors.CM_NOT_LOGIN){
				throw new ServiceException(JwtErrors.CM_NOT_LOGIN, e);
			}*/
			throw e;
		} catch (Exception e){
			throw new ServiceException(JwtErrors.CM_LOGIN_UNKNOW_ERR, e);
		}
		
		if(!userOpt.isPresent()){
			throw new ServiceException(JwtErrors.CM_NOT_LOGIN);
		}
		return true;
	}

	public void setAuthHeaderName(String authHeaderName) {
		this.authHeaderName = authHeaderName;
	}

	public void setCanBeAnonymous(boolean canBeAnonymous) {
		this.canBeAnonymous = canBeAnonymous;
	}

	@Override
	public String toString() {
		return "JwtMvcInterceptor [authHeaderName=" + authHeaderName
				+ ", canBeAnonymous=" + canBeAnonymous + "]";
	}

}

@@ -1078,24 +1078,26 @@
 erName);%0A%09%09%7D
+/*
  catch (Serv
@@ -1117,18 +1117,16 @@
 e) %7B%0A%09%09%09
-/*
 if(e.get
@@ -1279,47 +1279,109 @@
 %09%09%09%7D
-*/
 %0A%09%09
-%09throw e;%0A%09%09%7D catch (Exception e)%7B
+%7D*/ catch (Exception e)%7B%0A%09%09%09if(e instanceof ServiceException)%7B%0A%09%09%09%09throw (ServiceException)e;%0A%09%09%09%7D
 %0A%09%09%09

fix jwt login