gpt4 book ai didi

jsp - Spring Security 更改请求 URI

转载 作者:行者123 更新时间:2023-12-04 12:51:29 26 4
gpt4 key购买 nike

我将 Spring Security 集成到现有的 Spring Boot 项目中(版本:1.5.3.RELEASE)。

在集成之前,我们通过扩展 HandlerInterceptorAdapater 的 preHandle 方法中的 getRequestURI 从请求中获取重定向信息。

请求 URI 正确指向他们的路径(例如:/admin/login)。

集成后,请求URI指向jsp的完整路径。

此外,我们还向 ConfigurableApplicationContext 注册了一个 ContextUtil 类,以便进一步检查 URI。在这个类中,我们像这样获取请求:

public HttpServletRequest getCurrentRequest()
{
final ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes();
return servletRequestAttributes.getRequest();
}

但 URI 也指向它在 /WEB-INF/ 下的“物理路径”

例如:
GET 请求指向 /WEB-INF/pages/admin/admin_login.jsp :

我的 WebSecurityConfig类是:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
//jeden Aufruf akzeptieren. Authorisierung und
Authentifizierung von Spring Security wird nicht genutzt
http.authorizeRequests().antMatchers("/").permitAll();
}

@Override
public void configure(WebSecurity web) throws Exception
{
web.ignoring().antMatchers("/resources/**", "/css/**", "/js/**",
"/img/**", "resources/*", "/WEB-INF/**").and().debug(true);
}
}

相关 applicationContext.xml部分:
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="classpath:/WEB-INF/resources/" />

<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean class="de.abc.xyu.zzz.interceptor.RedirectInterceptor" />
</mvc:interceptors>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
<property name="redirectHttp10Compatible" value="false" />
</bean>

Spring Security 的调试日志:

收到 GET '/admin/login' 的请求:

org.apache.catalina.connector.RequestFacade@70ad489

servletPath:/admin/login pathInfo:null headers: host: localhost:8081 connection: keep-alive cache-control: max-age=0 user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 upgrade-insecure-requests: 1 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 referer: http://localhost:8081/admin/login accept-encoding: gzip, deflate, br accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 cookie: JSESSIONID=AE07684D485DA698F1AA4DFE056D5B3A; JSESSIONID=0819B947A685FE3362F23E39CE999D3B

Security filter chain: [ WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter HeaderWriterFilter CsrfFilter
LogoutFilter RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter SessionManagementFilter
ExceptionTranslationFilter FilterSecurityInterceptor ]


[http-nio-8081-exec-1] INFO Spring Security Debugger -




收到 GET '/WEB-INF/pages/admin/admin_login.jsp' 的请求:

SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.context.HttpSessionSecurityContextRepository$Servlet3SaveToSessionRequestWrapper@2eac9514]

servletPath:/WEB-INF/pages/admin/admin_login.jsp pathInfo:null headers: host: localhost:8081 connection: keep-alive cache-control: max-age=0 user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 upgrade-insecure-requests: 1 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 referer: http://localhost:8081/admin/login accept-encoding: gzip, deflate, br accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 cookie: JSESSIONID=AE07684D485DA698F1AA4DFE056D5B3A; JSESSIONID=0819B947A685FE3362F23E39CE999D3B

Security filter chain: [] empty (bypassed by security='none')



为什么请求指向它在/WEB-INF/pages/login.jsp 下的物理路径而不是它的解析路径,我们如何实现它,我们如何获得“正确”的 URI?

最佳答案

最终这对我有用:

final ServletRequestAttributes servletRequestAttributes = 
(ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();

System.out.println("REQUEST URI: " +
servletRequestAttributes.getRequest()
.getAttribute("javax.servlet.forward.request_uri"));

这给出了真正的请求 URI,而不是它在/WEB-INF/下的“物理路径”。

关于jsp - Spring Security 更改请求 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48933005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com