当前位置: 首页 > 知识库问答 >
问题:

Spring Security更改请求URI

慕震博
2023-03-14

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

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

例如: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>

安全筛选器链:[WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter HeaderWriterFilter CSrffilter
LogoutFilter RequestCacheawareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter SessionManagementFilter
ExceptionTranslationFilter FilterSecurityInterceptor]

[http-nio-8081-exec-1]信息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 headers:host:localhost:8081连接:keep-alive cache-control:max-age=0用户-代理:mozilla/5.0(x11;Linux x86_64)applewebkit/537.36(KHTML,像Gecko)chrome/62.0.322.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 cookie:JSessionID=AE07684D485DA698F1AA4DFE056D5B3A;JSESSIONID=0819B947A685FE3362F23E39CE999D3B

安全筛选器链:[]空(由Security='none'绕过)

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

共有1个答案

帅煌
2023-03-14

最后这对我起作用了:

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

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

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

 类似资料:
  • 我正在尝试使用JSoup访问java中的在线api。通过我的浏览器,我可以很好地导航到url,并得到json数据作为回报。但是,如果我使用Jsoup,url会发生更改,并且会从中删除“/api”。例如:https://www.onehash.com/api/archived_contest/122/我可以通过浏览器很好地打开它,但如果我使用jsoup连接到它,url就会变成https://www.

  • 问题内容: 我想向页面URL 添加一些参数。我正在加载这样的内容: 如何更改具有相同参数的URL ? 问题答案: 您可以使用HTML5 pushState API来实现。但是,只有最新的浏览器才支持它,而IE9甚至不支持IE。没有办法做到这一点。但是,有一个约定,您将使用location.hash来指示页面的状态已被ajax请求修改的页面。有关如何使用URL散列来表示你的页面上状态的更多信息,你可

  • 问题内容: 我有登录功能 此函数用于将变量保存到会话中, 但是当新请求检查用户是否登录时 然后返回总是“注销”,因为会话已更改。我使用Redis来存储会话,我认为这是Redis的错误,因为当我停止使用Redis时,可以,请帮帮我! 问题答案: 最好的处理方式是始终让Express处理它(如果可以的话)。 https://flaviocopes.com/express-sessions/(更新了会话

  • 是否可以更改事件接收的对象的标头? 两次尝试: > 修改现有标题: 失败,原因是。 创建新的请求对象: 失败与

  • 问题内容: 我正在使用JQuery向我自己的Web服务发出AJAX请求。我需要为HTTP-AJAX-Request 设置或修改HTTP- Header,如何才能最简单地做到这一点? 我尝试了某些用户提供的使用方法来设置User- Agent的提示,但这不起作用。实际上,它确实适用于其他新创建的标头(如),但不适用于。 谢谢! 问题答案: 这完全是不可能的,不允许您更改XMLHttpRequests

  • 一、修改请求 在上一步中,我们恢复了截获的请求,没有进行任何更改。当我们在将请求转发到目的地之前修改被拦截的请求时,就会发挥拦截的全部功能。您可以继续上一步中的窗口和已配置的拦截规则。 欢迎来到mitmproxy教程。在本课程中,我们将介绍对截获请求的修改。 点击查看教程 二、影片内容 欢迎来到mitmproxy教程。在本课程中,我们将介绍对截获请求的修改。 我们配置和使用与上一教程中相同的拦截规