spring-mvc 相关问题

基于模型 - 视图 - 控制器(MVC)模式构建Java Web应用程序的框架。它从底层视图技术中提升灵活和分离的代码。

Spring boot v3.2.1 迁移后,Rest 端点无法工作

我正在将所有现有的 Spring Boot 应用程序从版本 2.7.4 迁移到 3.2.1。 我有一个奇怪的观察。所有以 / 结尾的端点都无法从 UI 访问以及...

回答 1 投票 0

Spring Boot 3 中请求的资源上不存在“Access-Control-Allow-Origin”标头

这是我的 CORSConfig 类 导入 org.springframework.context.annotation.Bean; 导入 org.springframework.context.annotation.Configuration; 导入 org.springframework.web.servlet.config.annotation.

回答 1 投票 0

Spring boot v3.2.1 迁移后,现有属性不再起作用

我正在将所有现有的 Spring Boot 应用程序从版本 2.7.4 迁移到 3.2.1。所有应用程序都在 application.yml 文件中定义了 50 多个属性。有 4 个

回答 1 投票 0

Spring Security 3.2 CSRF 对特定 URL 禁用

使用 Spring security 3.2 在我的 Spring MVC 应用程序中启用了 CSRF。 我的 spring-security.xml 使用 Spring security 3.2 在我的 Spring MVC 应用程序中启用了 CSRF。 我的 spring-security.xml <http> <intercept-url pattern="/**/verify" requires-channel="https"/> <intercept-url pattern="/**/login*" requires-channel="http"/> ... ... <csrf /> </http> 尝试对请求 URL 中包含“验证”的请求禁用 CSRF。 MySecurityConfig.java @Configuration @EnableWebSecurity public class MySecurityConfig extends WebSecurityConfigurerAdapter { private CsrfMatcher csrfRequestMatcher = new CsrfMatcher(); @Override public void configure(HttpSecurity http) throws Exception { http.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher); } class CsrfMatcher implements RequestMatcher { @Override public boolean matches(HttpServletRequest request) { if (request.getRequestURL().indexOf("verify") != -1) return false; else if (request.getRequestURL().indexOf("homePage") != -1) return false; return true; } } } Csrf 过滤器验证从“验证”提交的 CSRF 令牌,并且当我从 http 向 https 提交请求时抛出无效令牌异常 (403)。在这种情况下如何禁用 csrf 令牌身份验证? 我知道这不是一个直接的答案,但是人们(像我一样)在搜索此类问题时通常不会指定 spring 的版本。 因此,由于 Spring Security 存在一种方法,可以忽略某些路由: 以下内容将确保 CSRF 保护被忽略: 任何 GET、HEAD、TRACE、OPTIONS(这是默认值) 我们还明确声明忽略任何以“/sockjs/”开头的请求 http .csrf() .ignoringAntMatchers("/sockjs/**") 。和() ... 我希望我的回答可以帮助别人。我发现这个问题正在搜索How to disable CSFR for Specific URLs in Spring Boot。 我使用了此处描述的解决方案: http://blog.netgloo.com/2014/09/28/spring-boot-enable-the-csrf-check-selectively-only-for-some-requests/ 这是 Spring Security 配置,允许我禁用某些 URL 上的 CSFR 控制: @Configuration @EnableWebMvcSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { // Build the request matcher for CSFR protection RequestMatcher csrfRequestMatcher = new RequestMatcher() { // Disable CSFR protection on the following urls: private AntPathRequestMatcher[] requestMatchers = { new AntPathRequestMatcher("/login"), new AntPathRequestMatcher("/logout"), new AntPathRequestMatcher("/verify/**") }; @Override public boolean matches(HttpServletRequest request) { // If the request match one url the CSFR protection will be disabled for (AntPathRequestMatcher rm : requestMatchers) { if (rm.matches(request)) { return false; } } return true; } // method matches }; // new RequestMatcher // Set security configurations http // Disable the csrf protection on some request matches .csrf() .requireCsrfProtectionMatcher(csrfRequestMatcher) .and() // Other configurations for the http object // ... return; } // method configure @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { // Authentication manager configuration // ... } } 它适用于 Spring Boot 1.2.2(和 Spring Security 3.2.6)。 我正在使用 Spring Security v4.1。经过大量阅读和测试后,我使用 XML 配置禁用了特定 URL 的 CSRF 安全功能。 <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <http pattern="/files/**" security="none" create-session="stateless"/> <http> <intercept-url pattern="/admin/**" access="hasAuthority('GenericUser')" /> <intercept-url pattern="/**" access="permitAll" /> <form-login login-page="/login" login-processing-url="/login" authentication-failure-url="/login" default-target-url="/admin/" password-parameter="password" username-parameter="username" /> <logout delete-cookies="JSESSIONID" logout-success-url="/login" logout-url="/admin/logout" /> <http-basic /> <csrf request-matcher-ref="csrfMatcher"/> </http> <beans:bean id="csrfMatcher" class="org.springframework.security.web.util.matcher.OrRequestMatcher"> <beans:constructor-arg> <util:list value-type="org.springframework.security.web.util.matcher.RequestMatcher"> <beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"> <beans:constructor-arg name="pattern" value="/rest/**"/> <beans:constructor-arg name="httpMethod" value="POST"/> </beans:bean> <beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"> <beans:constructor-arg name="pattern" value="/rest/**"/> <beans:constructor-arg name="httpMethod" value="PUT"/> </beans:bean> <beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"> <beans:constructor-arg name="pattern" value="/rest/**"/> <beans:constructor-arg name="httpMethod" value="DELETE"/> </beans:bean> </util:list> </beans:constructor-arg> </beans:bean> //... </beans:bean> 通过上述配置,我仅对以 /rest/ 开头的所有 URL 的 POST|PUT|DELETE 请求启用 CSRF 安全性。 显式禁用特定 url 模式并启用某些 url 模式。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class SecurityConfig { @Configuration @Order public static class GeneralWebSecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/rest/**").and() .authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/home/**","/search/**","/geo/**").authenticated().and().csrf() .and().formLogin().loginPage("/login") .usernameParameter("username").passwordParameter("password") .and().exceptionHandling().accessDeniedPage("/error") .and().sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(true); } } } <http ...> <csrf request-matcher-ref="csrfMatcher"/> <headers> <frame-options policy="SAMEORIGIN"/> </headers> ... </http> <b:bean id="csrfMatcher" class="AndRequestMatcher"> <b:constructor-arg value="#{T(org.springframework.security.web.csrf.CsrfFilter).DEFAULT_CSRF_MATCHER}"/> <b:constructor-arg> <b:bean class="org.springframework.security.web.util.matcher.NegatedRequestMatcher"> <b:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher"> <b:constructor-arg value="/chat/**"/> </b:bean> </b:bean> </b:constructor-arg> </b:bean> 的意思是 http .csrf() // ignore our stomp endpoints since they are protected using Stomp headers .ignoringAntMatchers("/chat/**") 示例来自: https://docs.spring.io/spring-security/site/docs/4.1.x/reference/htmlsingle/ 在 Spring Security 6 中,应使用采用 csrf 参数的 Customizer 方法。配置示例: @Bean SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.csrf(csrf -> csrf.ignoringRequestMatchers("/somepath", "/other/**")); return http.build(); } 使用安全性=“无”。 例如在 spring-security-config.xml 中 <security:intercept-url pattern="/*/verify" security="none" />

回答 7 投票 0

Spring Security 6 中使用 securityMatcher 时默认 /logout 不起作用

在 Spring Security 6 中(特别是 Spring Boot 3.2,在我的例子中是带有 Thymeleaf 的 Spring MVC),当使用 securityMatcher 时,默认的 /logout POST 或 GET 将停止工作。 @豆 公开

回答 1 投票 0

非现有用户访问被拒绝(Spring-Boot JDBC 驱动程序)

我正在学习有关 Spring Boot 的教程,每次我尝试运行该应用程序时都会收到以下错误,事实是在 application.properties 中我传递了正确的用户名,所以我不...

回答 1 投票 0

使用restTemplate使用Html响应

我想使用restTemplate使用html响应并重新运行对我的html的响应,以便我可以将这些内容添加到我的html页面,但遇到错误尝试了很多替代方案,但不走运 我...

回答 2 投票 0

${pageContext.request.contextPath} 如何在本地和远程工作?

${pageContext.request.contextPath} 如何在本地和远程工作?

回答 1 投票 0

Spring Security(没有 SpringBoot):请求未经过身份验证/授权

我决定在进入 Spring Boot 之前先探索一下 Spring,以了解它是如何在基础上结合在一起的。我设置了一个非常基本的安全配置来验证所有路由,但它

回答 1 投票 0

将 Thymeleaf 片段作为参数传递给另一个片段时“选择器与选择器语法不匹配”

我想为所有页面提供一个共享模板,例如每个页面上都会张贴一个绿色框架。我有一个带有蓝色框架的索引页。所以我创建片段/整个页面....

回答 1 投票 0

activeMq redeliveryPolicy 从 tomee 传递到 spring 时被忽略

我在 tomee 1.7.1 和 activeMq 5.10 中部署了一个简单的 spring 应用程序。 我的问题是,我设定的重新投递政策似乎主要被忽略了重新投递的延迟。 我是我的 jms 听众,我立即...

回答 5 投票 0

使用Eclipse创建Maven Web APP项目,但没有获得所需的存储库结构

我是 Maven 和 Eclipse 的新手,我正在使用 Eclipse 创建一个 Maven Web APP 项目,但没有获得所需的存储库结构,这对我来说是这样的: 理想情况下必须有 Java 资源

回答 1 投票 0

Jackson多态反序列化:JsonMappingException

我想我有一个父类参数,它有 2 个子类 ComboParameter 和 IntegerParameter @JsonSubTypes({ @JsonSubTypes.Type(value = IntegerParameter.class, name = "integerPara...

回答 2 投票 0

重定向后如何保留请求参数?

我正在尝试解决发送空输入表单时出现的错误。 这是我的方法: @RequestMapping(值 = "/modifier.html", 方法 = RequestMethod.POST) 公共字符串修饰符(ModelMap映射,@

回答 3 投票 0

Spring boot - Application.properties 中的自定义变量

我有使用restful api 的Spring Boot 客户端。我可以使用 application.properties 中是否有任何关键条目,而不是在 java 类中硬编码 REST API 的 IP 地址? 还有...

回答 4 投票 0

Spring Boot Reactive:向控制器添加 @Validate 注解会导致相关测试失败

我目前正在 Sprig Boot(2) 反应式项目中工作。所有 CRUD 端点和测试都工作正常。然后我决定在创建待办事项时添加验证,为此我添加了注释...

回答 1 投票 0

spring-integration 5.5.18 消息网关超时

我使用 @MessagingGateway 是为了在 spring-mvc @Controller 和 spring-integration 之间架起桥梁。 @MessagingGateway 声明如下所示: 包a.b.c; 导入org.springframework。

回答 1 投票 0

Springboot:PathVariable 验证,其中条件 {variable}.isBlank/{variable}.isEmpty 不起作用

我有一个控制器,我想在其中验证 PathVariable (它不能为空)。例如,http://localhost:8080/api/details/expired-timeoff/ 我想验证路径变量...

回答 1 投票 0

Spring MVC HiddenHttpMethodFilter() 不会调用标有@PatchMapping的方法

我只是在学习,一切都按照我正在观看的课程进行。但是,在我的例子中,服务器返回 HTTP 405 消息请求方法“POST”不受支持。 问题:“更新&...

回答 1 投票 0

上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException

在 Eclipse 中成功运行我的 Spring Boot 应用程序(将 jason 发布到 Spring RESTful Web 服务的示例程序)。但是,当我使用 mvn package 命令获取 jar 文件时。得到了关注我...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.