开发者社区 问答 正文

因为配置了WebMvcConfigurationSupport监控页面登录的时候获取username

配置了以下代码后登录监控页面获取参数为null @configuration public class WebConfigurer extends WebMvcConfigurationSupport {

/** * reources that do not pass the interceptor * 设置全局变量 * * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // 静态文件 registry.addResourceHandler("/static/").addResourceLocations("classpath:/static/"); // swagger2页面 registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/").addResourceLocations("classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); }

T26.png

请求为:

T27.png

建议将登录参数修改为json形式,放入body体内。

原提问者GitHub用户1725636955

展开
收起
山海行 2023-07-05 19:20:51 102 分享 版权
3 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    通过反向分析原因,主要涉及到两方面:
    Spring MVC 的配置
    是否重写了addInterceptors()等方式,对所有请求增强。
    Spring Security 的配置
    是否对所有URL进行了权限控制。

    检查这两个方面可以帮助你定位原因:
    重写了Spring MVC的Interceptor
    使用了Spring Security全局拦截
    配置了过滤器获取登录信息
    应用存在安全漏洞
    误配置

    2023-07-30 19:43:34
    赞同 展开评论
  • 删除配置类WebMvcConfigurationSupport一切正常

    原回答者GitHub用户1725636955

    2023-07-06 10:52:00
    赞同 展开评论
  • 要在监控页面登录时获取用户名,你可以通过以下步骤实现:

    1. 创建一个类并继承WebMvcConfigurationSupport,以自定义配置Spring MVC的行为。

    2. 在这个类中,覆盖addViewControllers方法,添加一个视图控制器将监控页面映射到一个特定的URL路径。

    @Configuration
    public class WebConfig extends WebMvcConfigurationSupport {
        @Override
        protected void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/monitor").setViewName("monitor");
        }
    }
    
    1. 创建一个Controller处理监控页面的请求,并从HTTP请求中获取当前用户的用户名。你可以使用SecurityContextHolder来获取已认证用户的信息。
    @Controller
    public class MonitorController {
        @GetMapping("/monitor")
        public String monitorPage(Model model, HttpServletRequest request) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            String username = authentication.getName();
    
            // 将用户名传递给监控页面
            model.addAttribute("username", username);
    
            // 返回监控页面视图
            return "monitor";
        }
    }
    
    1. 创建一个监控页面模板(比如Thymeleaf或JSP),并在其中显示获取到的用户名。
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>监控页面</title>
    </head>
    <body>
        <h1>欢迎访问监控页面,<span th:text="${username}"></span></h1>
    </body>
    </html>
    

    现在,当访问/monitor路径时,你将会看到监控页面并显示当前用户的用户名。请确保在Spring配置中启用了Spring Security来处理用户认证和授权。

    2023-07-05 19:34:15
    赞同 展开评论
问答分类:
问答地址: