我正在開發一個簡單的Java EE應用程序,使用普通的jsp serlvets以及POJO
類和DAO
支持。在此實現訪問控制的最佳策略是什麼?Java EE中的訪問控制
我最初以爲使用filter
,但我不太清楚它的用法,那麼是否有任何簡單的編程方式可以實現?我的意思是通過使用properties
文件或其他方法?
另外我想保持我的應用程序的輕量級。所以請建議相同的解決方案?提前致謝!
我正在開發一個簡單的Java EE應用程序,使用普通的jsp serlvets以及POJO
類和DAO
支持。在此實現訪問控制的最佳策略是什麼?Java EE中的訪問控制
我最初以爲使用filter
,但我不太清楚它的用法,那麼是否有任何簡單的編程方式可以實現?我的意思是通過使用properties
文件或其他方法?
另外我想保持我的應用程序的輕量級。所以請建議相同的解決方案?提前致謝!
基本上ACL是在Java EE中通過在會話bean類或其p方法上使用@DeclareRoles和@RolesAllowed註釋來實現的程序安全。您還可以在您的部署描述符(web.xml)中使用元素來描述您的角色以及授權安全性。
這裏是Java EE tutorial exampple對程序安全
package converter.ejb;
import java.math.BigDecimal;
import javax.ejb.Stateless;
import java.security.Principal;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
@Stateless()
@DeclareRoles("TutorialUser")
public class ConverterBean{
@Resource SessionContext ctx;
private BigDecimal yenRate = new BigDecimal("89.5094");
private BigDecimal euroRate = new BigDecimal("0.0081");
@RolesAllowed("TutorialUser")
public BigDecimal dollarToYen(BigDecimal dollars) {
BigDecimal result = new BigDecimal("0.0");
Principal callerPrincipal = ctx.getCallerPrincipal();
if (ctx.isCallerInRole("TutorialUser")) {
result = dollars.multiply(yenRate);
return result.setScale(2, BigDecimal.ROUND_UP);
} else {
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
@RolesAllowed("TutorialUser")
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal result = new BigDecimal("0.0");
Principal callerPrincipal = ctx.getCallerPrincipal();
if (ctx.isCallerInRole("TutorialUser")) {
result = yen.multiply(euroRate);
return result.setScale(2, BigDecimal.ROUND_UP);
} else {
return result.setScale(2, BigDecimal.ROUND_UP);
}
}
}
你也可以做到這一點在你的servlet通過HttpServletRequest的」登錄,註銷和驗證的身份驗證方法,然後使用getUserPrincipal的isUserInRole和對ACL。然後,您需要在web.xml中添加到servlet的描述中,以引用在web.xml中的元素中聲明的角色。這裏是來自Java EE turorial的ACL部分的例子。
package enterprise.programmatic_login;
import java.io.*;
import java.net.*;
import javax.annotation.security.DeclareRoles;
import javax.servlet.*;
import javax.servlet.http.*;
@DeclareRoles("javaee6user")
public class LoginServlet extends HttpServlet {
/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String userName = request.getParameter("txtUserName");
String password = request.getParameter("txtPassword");
out.println("Before Login" + "<br><br>");
out.println("IsUserInRole?.."
+ request.isUserInRole("javaee6user")+"<br>");
out.println("getRemoteUser?.." + request.getRemoteUser()+"<br>");
out.println("getUserPrincipal?.."
+ request.getUserPrincipal()+"<br>");
out.println("getAuthType?.." + request.getAuthType()+"<br><br>");
try {
request.login(userName, password);
} catch(ServletException ex) {
out.println("Login Failed with a ServletException.."
+ ex.getMessage());
return;
}
out.println("After Login..."+"<br><br>");
out.println("IsUserInRole?.."
+ request.isUserInRole("javaee6user")+"<br>");
out.println("getRemoteUser?.." + request.getRemoteUser()+"<br>");
out.println("getUserPrincipal?.."
+ request.getUserPrincipal()+"<br>");
out.println("getAuthType?.." + request.getAuthType()+"<br><br>");
request.logout();
out.println("After Logout..."+"<br><br>");
out.println("IsUserInRole?.."
+ request.isUserInRole("javaee6user")+"<br>");
out.println("getRemoteUser?.." + request.getRemoteUser()+"<br>");
out.println("getUserPrincipal?.."
+ request.getUserPrincipal()+"<br>");
out.println("getAuthType?.." + request.getAuthType()+"<br>");
} finally {
out.close();
}
}
}
請參閱提供的Java EE教程鏈接以獲取更完整的示例和解釋。
如果您不打算使用某些API(例如spring security或apache shiro),則可以使用網頁過濾器。
在你的過濾器實現,如果你要授予訪問權限,只需撥打
chain.doFilter(request, response);
將使用正常處理的要求,否則,將用戶重定向到另一個頁面
response.sendRedirect(Url);
是不錯的選擇