配置了以下代码后登录监控页面获取参数为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); }
请求为:
建议将登录参数修改为json形式,放入body体内。
原提问者GitHub用户1725636955
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
通过反向分析原因,主要涉及到两方面:
Spring MVC 的配置
是否重写了addInterceptors()等方式,对所有请求增强。
Spring Security 的配置
是否对所有URL进行了权限控制。
检查这两个方面可以帮助你定位原因:
重写了Spring MVC的Interceptor
使用了Spring Security全局拦截
配置了过滤器获取登录信息
应用存在安全漏洞
误配置
要在监控页面登录时获取用户名,你可以通过以下步骤实现:
创建一个类并继承WebMvcConfigurationSupport
,以自定义配置Spring MVC的行为。
在这个类中,覆盖addViewControllers
方法,添加一个视图控制器将监控页面映射到一个特定的URL路径。
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/monitor").setViewName("monitor");
}
}
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";
}
}
<!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来处理用户认证和授权。