CSS和JS在应用Servlet过滤器后不起作用。

huangapple go评论89阅读模式
英文:

CSS and JS not working after applaying servelt filter

问题

这是您提供的代码的翻译部分:

这是我第一次使用dofilter函数来对我的jsp web应用程序进行身份验证一旦添加它CSS和JS在任何页面上都无法工作我一遍又一遍地搜索但无法将解决方案应用到我的代码中
以下是dofilter方法

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;

    String servletPath = request.getServletPath();

    // 存储在会话中的用户信息。
    // (成功登录后)。
    UserAccount loginedUser = AppUtils.getLoginedUser(request.getSession());

    if (servletPath.equals("/login")) {
        chain.doFilter(request, response);
        return;
    }

    HttpServletRequest wrapRequest = request;

    if (loginedUser != null) {
        // 用户名
        String userName = loginedUser.getUserName();

        // 角色
        List<String> roles = loginedUser.getRoles();

        // 用userName和Roles信息包装旧请求。
        wrapRequest = new UserRoleRequestWrapper(userName, roles, request);
    }

    // 必须登录的页面。
    if (SecurityUtils.isSecurityPage(request)) {

        // 如果用户未登录,
        // 重定向到登录页面。
        if (loginedUser == null) {

            String requestUri = request.getRequestURI();

            // 存储当前页面以在成功登录后重定向。
            int redirectId = AppUtils.storeRedirectAfterLoginUrl(request.getSession(), requestUri);

            response.sendRedirect(wrapRequest.getContextPath() + "/login?redirectId=" + redirectId);
            return;
        }

        // 检查用户是否具有有效的角色?
        boolean hasPermission = SecurityUtils.hasPermission(wrapRequest);
        if (!hasPermission) {

            RequestDispatcher dispatcher //
                    = request.getServletContext().getRequestDispatcher("/accessDeniedView.jsp");

            dispatcher.forward(request, response);
            return;
        }
    }

    chain.doFilter(wrapRequest, response);
}

以下是页面中的CSS链接:

<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">

<link
    href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
    rel="stylesheet">
英文:

it is the first time i use dofilter function for authenticating my jsp web application once added it and CSS and JS not working on any page, I made search over and over but can not apply the solutions in my code
find below dofilter method.

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String servletPath = request.getServletPath();
// User information stored in the Session.
// (After successful login).
UserAccount loginedUser = AppUtils.getLoginedUser(request.getSession());
if (servletPath.equals(&quot;/login&quot;)) {
chain.doFilter(request, response);
return;
}
HttpServletRequest wrapRequest = request;
if (loginedUser != null) {
// User Name
String userName = loginedUser.getUserName();
// Roles
List&lt;String&gt; roles = loginedUser.getRoles();
// Wrap old request by a new Request with userName and Roles information.
wrapRequest = new UserRoleRequestWrapper(userName, roles, request);
}
// Pages must be signed in.
if (SecurityUtils.isSecurityPage(request)) {
// If the user is not logged in,
// Redirect to the login page.
if (loginedUser == null) {
String requestUri = request.getRequestURI();
// Store the current page to redirect to after successful login.
int redirectId = AppUtils.storeRedirectAfterLoginUrl(request.getSession(), requestUri);
response.sendRedirect(wrapRequest.getContextPath() + &quot;/login?redirectId=&quot; + redirectId);
return;
}
// Check if the user has a valid role?
boolean hasPermission = SecurityUtils.hasPermission(wrapRequest);
if (!hasPermission) {
RequestDispatcher dispatcher //
= request.getServletContext().getRequestDispatcher(&quot;/accessDeniedView.jsp&quot;);
dispatcher.forward(request, response);
return;
}
}
chain.doFilter(wrapRequest, response);
}

also find CSS link in pages

&lt;link href=&quot;vendor/fontawesome-free/css/all.min.css&quot; rel=&quot;stylesheet&quot;
type=&quot;text/css&quot;&gt;
&lt;link
href=&quot;https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i&quot;
rel=&quot;stylesheet&quot;&gt;

答案1

得分: 0

我终于在深入项目时找到了问题所在,与从Login servelt中移除"/"有关,并且应该是@WebServlet({ "/login" })。

英文:

i finally found the issue while deluging the project and it was related to remove "/" from Login servelt and to be @WebServlet({ "/login" })

huangapple
  • 本文由 发表于 2020年9月10日 00:13:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定