我正在重组一个非常老的项目。我使用Facade静态类将逻辑与前端分离。我还使用工作单元类实现了存储库模式。这个想法是,前端只会在立面上进行简单的请求。然后,Facade处理所有内容(通过子系统),并且这些子类将一起使用工作单元。前端永远不会接触解决方案中的Persistence项目。
我想知道如何应对简单的要求。就像从表中获取特定行一样。现在,我必须为每个表(250个以上)为此做一个函数。接下来,如果我要从表中获取所有行,则必须对所有表再次执行相同的操作。这是不可行的,我无法解决。将持久性项目作为对前端项目的依赖关系会破坏整个分层想法。
我该如何解决?
答案 0 :(得分:1)
所以我缺乏知识。我不知道您可以创建通用方法(与通用类相对)。因此,我向外观添加了一些默认的通用方法。像这样:
package com.iafox.auth;
import java.io.IOException;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.Container;
import org.apache.catalina.Globals;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.authenticator.AuthenticatorBase;
import org.apache.catalina.authenticator.Constants;
import org.apache.catalina.authenticator.DigestAuthenticator;
import org.apache.catalina.authenticator.FormAuthenticator;
import org.apache.catalina.authenticator.SSLAuthenticator;
import org.apache.catalina.authenticator.BasicAuthenticator;
import org.apache.catalina.connector.Request;
import org.apache.coyote.ActionCode;
import org.apache.tomcat.util.descriptor.web.LoginConfig;
public class SSLWithFORMFallback7 extends AuthenticatorBase {
FormAuthenticator formAuthenticator = new FormAuthenticator();
SSLAuthenticator sslAuthenticator = new SSLAuthenticator();
BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
DigestAuthenticator digestAuthenticator = new DigestAuthenticator();
protected boolean doAuthenticate(Request request, javax.servlet.http.HttpServletResponse httpResponse) throws IOException {
// Have we already authenticated someone?)
Principal principal = request.getUserPrincipal();
//String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
if (principal != null) {
// Associate the session with any existing SSO session in order
// to get coordinated session invalidation at logout
String ssoId = (String) request.getNote(Constants.REQ_SSOID_NOTE);
if (ssoId != null) {
associate(ssoId, request.getSessionInternal(true));
}
return (true);
}
// Get certificates from the request
boolean certAuth = true;
X509Certificate certs[] = (X509Certificate[]) request.getAttribute(Globals.CERTIFICATES_ATTR);
/*if ((certs == null) || (certs.length < 1)) {
request.getCoyoteRequest().action(ActionCode.REQ_SSL_CERTIFICATE, null);
certs = (X509Certificate[]) request.getAttribute(Globals.CERTIFICATES_ATTR);
}
if ((certs == null) || (certs.length < 1)) {
// No certificates
certAuth = false;
}*/
if ((certs == null) || (certs.length < 1)) {
// No certificates
certAuth = false;
System.out.println("NO CERTS");
} else {
request.getCoyoteRequest().action(ActionCode.REQ_SSL_CERTIFICATE, null);
certs = (X509Certificate[]) request.getAttribute(Globals.CERTIFICATES_ATTR);
System.out.println(String.valueOf(certs.length) + " CERTS");
}
// Delegate authentication request
boolean retval;
/*if (certAuth) {
retval = sslAuthenticator.authenticate(request, httpResponse, lc);
}
else*/
{
String servletPath = request.getServletPath();
if (servletPath == null) servletPath = "";
/*System.out.println("servletpath:" + request.getServletPath());
System.out.println("queryString:" + request.getQueryString());
System.out.println(request.getCoyoteRequest().getMimeHeaders().toString());
*/
if (servletPath.contains(".jsp") || servletPath.contains("j_security_check")) {
//System.out.println("start FormAuth");
retval = formAuthenticator.authenticate(request, httpResponse);
} else {
String authorization = request.getHeader("authorization");
if (authorization != null && authorization.toLowerCase().startsWith("basic ")) {
//System.out.println("start BasicAuth");
retval = basicAuthenticator.authenticate(request, httpResponse);
} else {
//System.out.println("start DigestAuth");
retval = digestAuthenticator.authenticate(request, httpResponse);
}
}
}
//System.out.println("Retval: " + retval + ", certAuth: " + certAuth);
return retval;
}
private String infoStr = null;
public String getInfo() {
if(null == infoStr) {
infoStr = this.getClass().getName();
}
return infoStr;
}
@Override
protected String getAuthMethod() {
return HttpServletRequest.CLIENT_CERT_AUTH;
// return HttpServletRequest.FORM_AUTH;
}
@Override
public void setContainer(Container container) {
super.setContainer(container);
sslAuthenticator.setContainer(container);
formAuthenticator.setContainer(container);
basicAuthenticator.setContainer(container);
digestAuthenticator.setContainer(container);
}
@Override
protected void initInternal() throws LifecycleException {
super.initInternal();
formAuthenticator.setAlwaysUseSession(alwaysUseSession);
formAuthenticator.init();
sslAuthenticator.setAlwaysUseSession(alwaysUseSession);
sslAuthenticator.init();
basicAuthenticator.setAlwaysUseSession(alwaysUseSession);
basicAuthenticator.init();
digestAuthenticator.setAlwaysUseSession(alwaysUseSession);
digestAuthenticator.init();
}
@Override
public void startInternal() throws LifecycleException{
super.startInternal();
formAuthenticator.start();
sslAuthenticator.start();
basicAuthenticator.start();
digestAuthenticator.start();
}
@Override
public void stopInternal() throws LifecycleException {
super.stopInternal();
formAuthenticator.stop();
sslAuthenticator.stop();
basicAuthenticator.stop();
digestAuthenticator.stop();
}
}
请注意,它会跳过存储库。对我来说,这是可以的,因为存储库中定义的方法更加复杂,并且永远不需要直接从前端使用。
我希望这对以后的所有人有帮助。