【问题标题】:Spring Security alters request URISpring Security 更改请求 URI
【发布时间】:2018-08-02 15:12:13
【问题描述】:

我将 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 连接:保持活动缓存控制:max-age=0 用户代理: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, 像 Gecko) Chrome/62.0.3202.94 Safari/537.36 upgrade-insecure-requests: 1 接受: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 推荐人:http://localhost:8081/admin/login 接受编码: gzip, deflate, br 接受语言:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 饼干:JSESSIONID=AE07684D485DA698F1AA4DFE056D5B3A; JSESSIONID=0819B947A685FE3362F23E39CE999D3B

安全过滤器链:[ WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter HeaderWriterFilter CsrfFilter
LogoutFilter RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter SessionManagementFilter
ExceptionTranslationFilter FilterSecurityInterceptor ]


[http-nio-8081-exec-1] INFO Spring Security 调试器 -


收到 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 标头:主机:本地主机:8081 连接:保持活动缓存控制: max-age=0 用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36(KHTML,如 Gecko)Chrome/62.0.3202.94 Safari/537.36 升级不安全请求:1 接受: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 推荐人:http://localhost:8081/admin/login 接受编码: gzip, deflate, br 接受语言:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7 饼干:JSESSIONID=AE07684D485DA698F1AA4DFE056D5B3A; JSESSIONID=0819B947A685FE3362F23E39CE999D3B

安全过滤器链:[] 空(被 security='none' 绕过)

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

【问题讨论】:

  • 我也面临同样的问题。你能解决这个问题吗?
  • 你能告诉我你在控制器中从哪里访问这个 url 吗?
  • @Manoj,是的,在 AbstractTagController 中,@可配置

标签: jsp spring-security uri


【解决方案1】:

最终这对我有用:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 2015-10-04
    • 2018-11-15
    • 2014-07-04
    • 2012-11-02
    • 2016-01-08
    • 2013-03-17
    • 2017-11-17
    相关资源
    最近更新 更多