Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the current login through custom HandlerMethodArgumentResolver in Springboot

2025-07-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to get the current login through the custom HandlerMethodArgumentResolver in Springboot. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The Springboot version is 2.0.5.release.

Generally, to get the current login in the controller method, we can customize the HandlerMethodArgumentResolver to achieve this, as follows:

List-1

@ Target (ElementType.PARAMETER) @ Retention (RetentionPolicy.RUNTIME) public @ interface LoginUser {}... public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {@ Override public boolean supportsParameter (MethodParameter methodParameter) {return methodParameter.getParameterType () .isAssignableFrom (UserVo.class) & & methodParameter.hasParameterAnnotation (LoginUser.class);} @ Override public Object resolveArgument (MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {return SpringHelper.getLoginUser () }}. @ Configurationpublic class WebSecurityConfig implements WebMvcConfigurer {@ Override public void addArgumentResolvers (List resolvers) {resolvers.add (new LoginUserHandlerMethodArgumentResolver ());}. Public class SpringHelper {public static HttpServletRequest getCurrentRequest () {HttpServletRequest request = null; try {RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes (); if (null==requestAttributes) {return null } request = ((ServletRequestAttributes) requestAttributes). GetRequest ();} catch (Exception e) {return request;} return request;} public static UserVo getLoginUser () {return (UserVo) getCurrentRequest (). GetSession (). GetAttribute (WebSecurityConfig.SESSION_KEY);}}

List-2

Public List queryIDInfo (@ LoginUser UserVo userVo) throws AicException {...}

In List-1, we customize LoginUserHandlerMethodArgumentResolver, and the method supportsParameter returns true to indicate that the parsing of this parameter is supported, and then resolveArgument is called to parse the parameter.

In List-2, we directly annotate the controller method with @ LoginUser, and then Spring automatically passes in the userVo.

The above is how to get the current login through custom HandlerMethodArgumentResolver in Springboot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report