在我的Java EE应用程序中,我通过JDBC领域实施了身份验证/自动化(第一次应用此解决方案)。使用JDBC领域进行身份验证
以下代码在成功登录时没有任何问题,问题是当我键入错误的凭据时:无论如何它都会登录,即使它捕获到ServletException(登录失败),这些代码行也不会执行在调试模式下):
request.setAttribute(“msg”,“Login in error”);
nextPage =“/errorPage.jsp”;
另一个奇怪的事情:无论什么我传递给
getServletContext()方法的getRequestDispatcher(下一页)的.forward(请求,响应 );
as nextPage(我试图把静态的“/errorPage.jsp”),它总是转发到index.jsp。
的Login.jsp
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
String nextPage = "/index.jsp";
try {
request.login(username, password);
}
catch (ServletException ex) {
request.setAttribute("msg", "Error in login");
nextPage = "/errorPage.jsp";
}
getServletContext().getRequestDispatcher(nextPage).forward(request, response);
}
}
的login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.logout();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Hi! You need to login.</h1>
<form method="POST" action="/MyApp/Login">
Usuario: <input type="text" name="username" /> Password: <input
type="password" name="password" /> <input type="submit"
value="Send" />
</form>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>MyApp</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>jdbcRealm</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/errorPage.jsp</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin stuff</web-resource-name>
<url-pattern>/admin/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>User stuff</web-resource-name>
<url-pattern>/user/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
在此之前,我试过container-managed security
溶液(与登录的表单动作调用j_security_check组件)。
登录正常工作与此(即使有错误的凭证),但我得到了另一个严重的问题之前,我没有:在一个用例,用户可以看到该项目正在开发,但它不应该能够看到其他用户的项目。我用下面的servlet实现了它,但问题在于(像其他解决方案一样),它跳过了一些指令(例如,在数据库中查找用户的指令),并且出现异常,重定向到错误页。
public class ViewUserProjects extends HttpServlet {
private static final long serialVersionUID = 1L;
public ViewUserProjects() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
DAO dao = (DAO) getServletContext().getAttribute("bd");
Principal p = request.getUserPrincipal();
String username = p.getName();
try {
User user = dao.getUserByName(name);
request.getSession().setAttribute("user", user);
ArrayList<Project> projects = new ArrayList<Project>();
tareas = ad.getUserProjects(Integer.parseInt(user.getId()));
request.setAttribute("projects", projects);
getServletContext().getRequestDispatcher(
"/user/viewProjects.jsp").forward(request,
response);
} catch (Exception ex) {
request.setAttribute("msg",
"Error");
getServletContext().getRequestDispatcher("/errorPage.jsp").forward(
request, response);
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
认证在通过JDBC领域https://docs.oracle.com/javaee/6/tutorial/doc/glxgo.html – user3673449