forked from microsphere-projects/microsphere-spring
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e22d28a
commit 327d974
Showing
3 changed files
with
72 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...t/java/io/microsphere/spring/webmvc/annotation/EnableWebMvcExtensionInterceptorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.microsphere.spring.webmvc.annotation; | ||
|
||
import io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor; | ||
import org.junit.Test; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import static io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor.MOCK_TOKEN_VALUE; | ||
import static io.microsphere.spring.webmvc.interceptor.IdempotentAnnotatedMethodHandlerInterceptor.TOKEN_HEADER_NAME; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* {@link EnableWebMvcExtension} Test with interceptors | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy<a/> | ||
* @see EnableWebMvcExtension | ||
* @since 1.0.0 | ||
*/ | ||
@ContextConfiguration(classes = { | ||
EnableWebMvcExtensionInterceptorsTest.class | ||
}) | ||
@EnableWebMvcExtension(handlerInterceptors = { | ||
IdempotentAnnotatedMethodHandlerInterceptor.class | ||
}) | ||
public class EnableWebMvcExtensionInterceptorsTest extends AbstractEnableWebMvcExtensionTest { | ||
|
||
@Test | ||
@Override | ||
public void test() throws Exception { | ||
this.mockMvc.perform(get("/echo/hello").header(TOKEN_HEADER_NAME, MOCK_TOKEN_VALUE)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json("[ECHO] : hello")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
*/ | ||
package io.microsphere.spring.webmvc.interceptor; | ||
|
||
import io.microsphere.logging.Logger; | ||
import io.microsphere.spring.web.event.HandlerMethodArgumentsResolvedEvent; | ||
import io.microsphere.spring.webmvc.IdempotentException; | ||
import io.microsphere.spring.webmvc.annotation.Idempotent; | ||
|
@@ -29,22 +30,30 @@ | |
import javax.servlet.http.HttpSession; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import static io.microsphere.logging.LoggerFactory.getLogger; | ||
import static java.util.Arrays.asList; | ||
|
||
/** | ||
* {@link AnnotatedMethodHandlerInterceptor} for {@link Idempotent} annotation | ||
* | ||
* @author <a href="mailto:[email protected]">Mercy</a> | ||
* @since 1.0.0 | ||
*/ | ||
public class IdempotentAnnotatedMethodHandlerInterceptor extends AnnotatedMethodHandlerInterceptor<Idempotent> implements ApplicationListener<HandlerMethodArgumentsResolvedEvent> { | ||
public class IdempotentAnnotatedMethodHandlerInterceptor extends AnnotatedMethodHandlerInterceptor<Idempotent> | ||
implements ApplicationListener<HandlerMethodArgumentsResolvedEvent> { | ||
|
||
private static final Logger logger = getLogger(IdempotentAnnotatedMethodHandlerInterceptor.class); | ||
|
||
public static final String TOKEN_HEADER_NAME = "_token_"; | ||
|
||
public static final String MOCK_TOKEN_VALUE = UUID.randomUUID().toString(); | ||
|
||
@Override | ||
protected boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, | ||
Idempotent idempotent) throws Exception { | ||
|
||
System.out.println(handlerMethod); | ||
System.out.println(idempotent); | ||
|
||
return true; | ||
} | ||
|
||
|
@@ -54,19 +63,14 @@ protected boolean preHandle(HttpServletRequest request, HttpServletResponse resp | |
public void onApplicationEvent(HandlerMethodArgumentsResolvedEvent event) { | ||
Method method = event.getMethod(); | ||
Object[] args = event.getArguments(); | ||
System.out.println("method : " + method + " , args : " + Arrays.asList(args)); | ||
WebRequest webRequest = event.getWebRequest(); | ||
logger.trace("The method : {} , args : {} , webRequest : {}", method, asList(args), webRequest); | ||
if (webRequest instanceof ServletWebRequest) { | ||
ServletWebRequest servletWebRequest = (ServletWebRequest) webRequest; | ||
HttpServletRequest request = servletWebRequest.getNativeRequest(HttpServletRequest.class); | ||
// HttpSession based on Spring Redis | ||
// Spring Session | ||
HttpSession httpSession = request.getSession(); | ||
String token = request.getHeader("token"); | ||
Object tokenValue = httpSession.getAttribute(token); | ||
if (tokenValue != null) { | ||
// | ||
throw new IdempotentException(""); | ||
String token = request.getHeader(TOKEN_HEADER_NAME); | ||
if (!Objects.equals(MOCK_TOKEN_VALUE, token)) { | ||
throw new IdempotentException("Illegal token"); | ||
} | ||
} | ||
} | ||
|