Search in sources :

Example 86 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project esup-papercut by EsupPortail.

the class ContextUserDetails method getAuthorities.

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String papercutContext = WebUtils.getContext(request);
        if (contextAuthorities.get(papercutContext) != null && !contextAuthorities.get(papercutContext).isEmpty()) {
            return contextAuthorities.get(papercutContext);
        }
    }
    return defaultAuthorities;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 87 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project esup-papercut by EsupPortail.

the class ContextUserDetails method getUsername.

@Override
public String getUsername() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String papercutContext = WebUtils.getContext(request);
        if (contextUids.get(papercutContext) != null && !contextUids.get(papercutContext).isEmpty()) {
            return contextUids.get(papercutContext);
        }
    }
    return username;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 88 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project esup-papercut by EsupPortail.

the class ContextUserDetails method getEmail.

public String getEmail() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String papercutContext = WebUtils.getContext(request);
        if (contextEmails.get(papercutContext) != null && !contextEmails.get(papercutContext).isEmpty()) {
            return contextEmails.get(papercutContext);
        }
    }
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 89 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project plumdo-work by wengwh.

the class AuthCheckAspect method doAuth.

@Around("webRequestAuth()&& webRequestNotAuth()")
public Object doAuth(ProceedingJoinPoint pjp) throws Throwable {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    String userId = null;
    String token = request.getHeader("Token");
    if (ObjectUtils.isEmpty(token)) {
        token = request.getParameter("token");
    }
    logger.info("token:" + token);
    if (ObjectUtils.isEmpty(token)) {
        exceptionFactory.throwAuthError(CoreConstant.HEADER_TOKEN_NOT_FOUND);
    }
    if (!token.startsWith("Bearer ")) {
        exceptionFactory.throwAuthError(CoreConstant.HEADER_TOKEN_ILLEGAL);
    }
    try {
        Claims claims = Jwts.parser().setSigningKey(CoreConstant.JWT_SECRET).parseClaimsJws(token.substring(7)).getBody();
        userId = claims.getId();
        if (ObjectUtils.isEmpty(userId)) {
            exceptionFactory.throwAuthError(CoreConstant.TOKEN_USER_ID_NOT_FOUND);
        }
        Date expiration = claims.getExpiration();
        if (expiration != null && expiration.before(new Date())) {
            exceptionFactory.throwAuthError(CoreConstant.TOKEN_EXPIRE_OUT);
        }
    } catch (Exception e) {
        exceptionFactory.throwAuthError(CoreConstant.TOKEN_AUTH_CHECK_ERROR);
    }
    try {
        Authentication.setToken(token);
        Authentication.setUserId(userId);
        return pjp.proceed(pjp.getArgs());
    } finally {
        Authentication.clear();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Claims(io.jsonwebtoken.Claims) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Date(java.util.Date) Around(org.aspectj.lang.annotation.Around)

Example 90 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project leopard by tanhaichao.

the class ImageDfsServiceImpl method isWeb.

/**
 * 是否web环境
 *
 * @return
 */
protected boolean isWeb() {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    if (attributes == null) {
        return false;
    }
    HttpServletRequest request = attributes.getRequest();
    if (request == null) {
        return false;
    }
    return true;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)204 HttpServletRequest (javax.servlet.http.HttpServletRequest)92 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)62 RequestAttributes (org.springframework.web.context.request.RequestAttributes)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)32 Test (org.junit.jupiter.api.Test)28 lombok.val (lombok.val)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 Test (org.junit.Test)18 AuthChecker (com.code.server.login.anotation.AuthChecker)17 HttpSession (javax.servlet.http.HttpSession)12 Before (org.junit.Before)12 HashMap (java.util.HashMap)11 Method (java.lang.reflect.Method)8 Date (java.util.Date)7 AbstractTracingSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan)7 TraceSegment (org.apache.skywalking.apm.agent.core.context.trace.TraceSegment)7 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)6 Map (java.util.Map)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6