英文:
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("/login")) {
chain.doFilter(request, response);
return;
}
HttpServletRequest wrapRequest = request;
if (loginedUser != null) {
// User Name
String userName = loginedUser.getUserName();
// Roles
List<String> 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() + "/login?redirectId=" + redirectId);
return;
}
// Check if the user has a valid role?
boolean hasPermission = SecurityUtils.hasPermission(wrapRequest);
if (!hasPermission) {
RequestDispatcher dispatcher //
= request.getServletContext().getRequestDispatcher("/accessDeniedView.jsp");
dispatcher.forward(request, response);
return;
}
}
chain.doFilter(wrapRequest, response);
}
also find CSS link in pages
<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">
答案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" })
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论