[英]How JSP page should check authentication
I am new to web programming.我是网络编程的新手。 I am asking a common pattern to do things like checking authentication.
我要求使用通用模式来执行检查身份验证等操作。 Here is the scenario:
这是场景:
The website has a login page for visitors.该网站有一个供访问者使用的登录页面。 It will take username and encrypted password and sent them to server, then get either a error code (username/password doesn't match)or an auth key from the server.
它将获取用户名和加密密码并将它们发送到服务器,然后从服务器获取错误代码(用户名/密码不匹配)或身份验证密钥。 When the user logged in successfully, I want the website automatically jump to the
main.jsp
page that presents the main functionality of the website.当用户登录成功后,我想让网站自动跳转到展示网站主要功能的
main.jsp
页面。
In this case, I want main.jsp
check the user authentication.在这种情况下,我希望
main.jsp
检查用户身份验证。 That is, I don't want such thing happens like user can directly open www.example.com/main.jsp
, and if they did thing like this, I want to redirect them to login page.也就是说,我不希望像用户可以直接打开
www.example.com/main.jsp
这样的事情发生,如果他们做了这样的事情,我想将他们重定向到登录页面。
So how could I pass authentication information across page, and how could I prevent user from directly accessing the main.jsp
without login?那么如何跨页面传递身份验证信息,以及如何防止用户在
main.jsp
情况下直接访问main.jsp
呢? Do I need to use session or anything?我需要使用 session 或任何东西吗?
you could try using filters :您可以尝试使用过滤器:
Filter can pre-process a request before it reaches a servlet, post-process a response leaving a servlet, or do both.
过滤器可以在请求到达 servlet 之前对其进行预处理,对离开 servlet 的响应进行后处理,或者两者都做。 Filters can intercept, examine, and modify requests and responses.
过滤器可以拦截、检查和修改请求和响应。
NOTE: be sure to add a session attribute once your user is logged in, you can use that session attribute on the filter注意:确保在您的用户登录后添加会话属性,您可以在过滤器上使用该会话属性
on your login.jsp add:在您的login.jsp 上添加:
session.setAttribute("LOGIN_USER", user);
//user entity if you have or user type of your user account...
//if not set then LOGIN_USER will be null
web.xml网页.xml
<filter>
<filter-name>SessionCheckFilter</filter-name>
<filter-class>yourjavapackage.SessionCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionCheckFilter</filter-name>
<!--url-pattern>/app/*</url-pattern-->
<url-pattern>/main.jsp</url-pattern> <!-- url from where you implement the filtering -->
</filter-mapping>
SessionCheckFilter.java SessionCheckFilter.java
public class SessionCheckFilter implements Filter {
private String contextPath;
@Override
public void init(FilterConfig fc) throws ServletException {
contextPath = fc.getServletContext().getContextPath();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if (req.getSession().getAttribute("LOGIN_USER") == null) { //checks if there's a LOGIN_USER set in session...
res.sendRedirect(contextPath + "/login.jsp"); //or page where you want to redirect
} else {
String userType = (String) req.getSession().getAttribute("LOGIN_USER");
if (!userType.equals("ADMIN")){ //check if user type is not admin
res.sendRedirect(contextPath + "/login.jsp"); //or page where you want to
}
fc.doFilter(request, response);
}
}
@Override
public void destroy() {
}
}
How JSP page should check authentication
JSP 页面应该如何检查身份验证
It shouldn't.不应该。 You should use Container Managed Authentication, and define the login/security in web.xml via URL patterns.
您应该使用容器管理身份验证,并通过 URL 模式在 web.xml 中定义登录/安全性。
Added by Glen Best:由 Glen Best 添加:
Eg Add something like this to web.xml:例如,在 web.xml 中添加类似的内容:
<security-constraint>
<display-name>GET: Employees Only</display-name>
<web-resource-collection>
<web-resource-name>Restricted Get</web-resource-name>
<url-pattern>/restricted/employee/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>Employee</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
This also works for me这也适用于我
<filter>
<filter-name>SessionCheckFilter</filter-name>
<filter-class>yourjavapackage.SessionCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SessionCheckFilter</filter-name>
<!--url-pattern>/app/*</url-pattern-->
<url-pattern>/main.jsp</url-pattern> <!-- url from where you implement the filtering -->
</filter-mapping>
public class SessionCheckFilter implements Filter {
private String contextPath;
@Override
public void init(FilterConfig fc) throws ServletException {
contextPath = fc.getServletContext().getContextPath();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if (req.getSession().getAttribute("LOGIN_USER") == null) { //checks if there's a LOGIN_USER set in session...
req.getRequestDispatcher("login.jsp").forward(req, resp); //or page where you want to redirect
} else {
String userType = (String) req.getSession().getAttribute("LOGIN_USER");
if (userType.equals("ADMIN")){ //check if user type is admin
fc.doFilter(request, response); it redirected towards main.jsp
}
}
}
@Override
public void destroy() {
}
}
如何使用:
String username = request.getRemoteUser();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.