gpt4 book ai didi

java - 拦截请求并在内部转发给 Controller ?

转载 作者:行者123 更新时间:2023-11-30 03:17:34 25 4
gpt4 key购买 nike

我有一个项目,分为不同的模块,例如一个网站和一个论坛。

该论坛位于:

http://example.com/[forum]/

例如可以是:

http://example.com/support/
http://example.com/helpme/
http://example.com/aforum/

该网站位于:

http://example.com/[site]/

例如可以是:

http://example.com/site1/
http://example.com/nice/
http://example.com/something/

[论坛]和[站点]部分是可变的。在我的数据库中,我查找“nice”是一个网站,“helpme”是一个论坛。

我的 ForumController 有一个 spring RequestMapping:

@RequestMapping(value = { "/{simpleTitle:[0-9a-z-]+}" }, method = RequestMethod.GET, produces = "text/html")
public void list(@PathVariable String simpleTitle, Model model, HttpServletRequest req, HttpServletResponse resp) {

我对网站有同样的东西,所以有一个 SiteController:

@RequestMapping(value = { "/{simpleTitle:[0-9a-z-]+}" }, method = RequestMethod.GET, produces = "text/html")
public void list(@PathVariable String simpleTitle, Model model, HttpServletRequest req, HttpServletResponse resp) {

这当然会变坏,因为 2 个具有相同请求映射的 Controller 不好。

我可以创建一个 FrontController,它具有上述请求映射,查找 simpleTitle 是什么(论坛或站点)并调用函数来显示论坛或站点。这有效。

但它不太像“spring”,也不太结构化。

是否可以“拦截”请求并自己在 Controller 上内部转发(或调用函数)?

这样我就可以拥有看起来简单的标题的拦截器,决定它是论坛还是网站,并“转发”/“调用”正确的 Controller 。

最佳答案

坦白说,我喜欢@Luiggi Mendoza 解决方案,但如果您想要替代方案,请使用如下内容:

            package eu.europa.acer.aris.ceremp.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;


@Component(value = "yourCustomFilter")
public class YourCustomFilter extends OncePerRequestFilter{

private static final Logger logger = LoggerFactory.getLogger(YourCustomFilter.class);

@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
//String[] path = request.getRequestURL().toString().split("/");

if (letPagePass(request.getRequestURL().toString()) == false)
{
// if request is bound to static resource like js//img do nothing, the filter chain will activate
if (letResourcePass(request.getRequestURL().toString()))
{

}
else
{
String[] urlInfos = obtainUrlAndParametersLast(request.getRequestURL().toString());
// last element will always give back last part including any parameter
// first element will always be a controller modifier
response.sendRedirect(request.getContextPath()+rebuildControllerPath(urlInfos));
return;
}
}
filterChain.doFilter(request, response);

}

private String rebuildControllerPath(String[] pathElement )
{
//do your thing here
if ("forum".equals(pathElement[0]))
{
String addenda = "/forumController/";
for (String singlePart: pathElement)
{
addenda = addenda+singlePart+"/";
}
return addenda;
}

}

// bind forceful redirect
public boolean letPagePass(String url)
{
// if you have some page that are naturally unique among controllers that you want always to process natively
String[] path = url.split("/");
if (path[path.length-2].startsWith("yourCertainUrlIgnoringParameters"))
{
return true;
}
// directcall
else if (path[path.length-2].startsWith("yourCertainUrlIgnoringParameters2"))
{
return true;
}
else
{
return false;
}
}

public boolean letResourcePass(String url)
{
String[] path = url.split("/");

/*
this assumes you have always a strict structure so your image//js//reource will always be in
https://domainname/a/lot/of/folder/img/actuaresource.png
or
https://domainname/a/lot/of/folder/js/actuaresource.js
etc
*/
//image pass
if (path[path.length-2].equals("img") || url.contains("/img/"))
{
return true;
}
//css pass
else if (path[path.length-2].equals("css") || url.contains("/css/"))
{
return true;
}
//js pass
else if (path[path.length-2].equals("js") || url.contains("/js/"))
{
return true;
}
else
{
return false;
}
}

@Override
public void destroy() {
}

}

并将以下 xml 片段添加到您的 web.xml 文件

        <!--  your fi Filter -->
<filter>
<filter-name>yourCustomFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>yourCustomFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

关于java - 拦截请求并在内部转发给 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32206398/

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