英文:
Spring RequestMapping with custom isLoggedIn parameter
问题
我已经在互联网上搜索了几个小时,但无法找到将自己的变量添加到请求映射中的方法。
我们正在使用自定义用户身份验证系统。我想为同一路径提供两个不同的控制器,这取决于用户是否已经通过身份验证。更具体地说,我想创建仅在用户未通过身份验证时才映射的控制器。
我如何实现类似这样的功能:(我想自己定义 isLoggedIn
)。
public class PageController {
@RequestMapping(value = "/page", isLoggedIn = false)
@ResponseBody
String getPage(){
return "Page content";
}
}
我希望如果用户未登录,则请求命中此控制器,并在用户已登录时回退到捕获所有内容。我可以接受使用拦截器、自定义注解、扩展RequestMapping或任何其他方法的解决方案。
英文:
I've been searching internet for several hours now, and cannot find a way to add my own variables into request mapping.
We are using a custom user authentication system. And I want to serve 2 different controllers for the same path depending on if the user is authenticated or not. More specifically, I want to create controllers that only mapped to if the user is not authenticated.
How can I achieve something like this: (I want to define isLoggedIn
myself).
public class PageController {
@RequestMapping(value = "/page", isLoggedIn = false)
@ResponseBody
String getPage(){
return "Page content";
}
}
I want request to hit this controller if user is not logged in, and fallback to catch all if user is logged in. I am open to solutions using Interceptors, Custom Annotations, or extending RequestMapping or anything else.
答案1
得分: 0
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
method = {RequestMethod.GET}
)
public @interface CustomGetRequestMapping {
@AliasFor(value = "value", annotation = RequestMapping.class)
String path();
boolean isLoggedIn() default false;
}
我猜这还不够,因为你需要编写拦截器来捕获已登录的用户。
<details>
<summary>英文:</summary>
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
method = {RequestMethod.GET}
)
public @interface CustomGetRequestMapping {
@AliasFor(value = "value", annotation = RequestMapping.class)
String path();
boolean isLoggedIn() default false;
}
I guess this is not enough because you have to write interceptor and catch the logged in user.
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论