gpt4 book ai didi

java - 使用 Servlet 监听器和过滤器的 HTTP 服务的编程配置

转载 作者:行者123 更新时间:2023-11-29 05:36:23 25 4
gpt4 key购买 nike

我尝试结合 VAADIN 和 shiro 在 OSGI 环境中实现 HttpService。所以我声明了以下 BundleActivator:

import java.util.logging.Level;

import org.eclipse.equinox.http.servlet.ExtendedHttpService;

import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
*
* @author nspecht
*/
public class BundleActivator extends some.package.BundleActivator implements ServiceTrackerCustomizer
{

/**
* Service tracker.
*/
private ServiceTracker tracker;

/**
* HttpService.
*/
private ExtendedHttpService httpService;

@Override
protected void afterStart(BundleContext bc, DependencyManager dm) {
this.tracker = new ServiceTracker(this.context, ExtendedHttpService.class, this);
this.tracker.open();
}

@Override
protected void beforeStop(BundleContext bc, DependencyManager dm) {
if (this.tracker != null) {
this.tracker.close();
this.tracker = null;
}
}

@Override
protected void afterBundleChanged(BundleEvent be) {

}

@Override
public Object addingService(ServiceReference sr) {
this.httpService = (ExtendedHttpService)this.context.getService(sr);
HttpContext httpContext = this.httpService.createDefaultHttpContext();
try {

MainServlet mainServlet = new MainServlet();
this.httpService.registerServlet(MainServlet.PATH, mainServlet, null, httpContext);

// TODO: add ServletContextListener and Filter

LoginServlet loginServlet = new LoginServlet();
this.httpService.registerServlet(LoginServlet.PATH, loginServlet, null, httpContext);

// TODO: add ServletContextListener and Filter

} catch (Exception ex) {
Logger.getLogger(BundleActivator.class.getName()).log(Level.SEVERE, null, ex);
}
return this.httpService;
}

@Override
public void modifiedService(ServiceReference sr, Object t) {
}

@Override
public void removedService(ServiceReference sr, Object t) {
this.httpService.unregister(MainServlet.PATH);
this.httpService.unregister(LoginServlet.PATH);
// TODO: remove ServletContextListener and Filter
}
}

MyServlets 是 VaadinServlets。现在我想将 Shiro 添加到我的应用程序中。为了使 shiro 正常工作,我必须将 EnvironmentLoaderListemer 和 ShiroFilter 添加到我的服务中。

如果我尝试以下操作:

mainServlet.mainServlet.getServletContext().addListener(...)

应用程序崩溃(因为上下文已经初始化)。

mainServlet.getServletContext().createListener(...)

不会抛出异常,但 ServletContextListener 也不起作用。httpService 没有以这种方式添加监听器的方法。过滤器有同样的问题,当监听器没有注册 EnvironmentLoader 时,httpService.registerFilter(...) 方法对于 ShiroFilter 是无用的。

是否有以编程方式添加 Shiro 的解决方法?

有没有办法为我的 Servlet 添加监听器和过滤器?

最佳答案

终于找到了解决办法。我创建了自己的 ShiroFilter 并像往常一样将其添加到我的 httpService 中:

ServletFilter filter = new ServletFilter();
this.httpService.registerFilter("/", filter, null, httpContext);

ServletFilter 也很简单:

public class ServletFilter extends IniShiroFilter
{
@Override
protected Ini loadIniFromConfig() {
return new MyIni();
}

@Override
protected Ini loadIniFromPath() {
return this.loadIniFromConfig();
}
}

最后是 MyIni 类:

public class MyIni extends Ini
{
/**
* Constructor.
*/
public MyIni() {
super();
// Set a class loader, which is able to resolve your classes!
Thread.currentThread().setContextClassLoader(new BundleWideClassLoader());
this.config();

}

@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append("# ").append(this.getClass().getCanonicalName()).append("\n");

for (String section : this.getSectionNames()) {
buf.append("\n[").append(section).append("]\n");
Section s = this.getSection(section);
for (String key : s.keySet()) {
buf.append(key).append(" = ").append(s.get(key)).append("\n");
}
}

return buf.toString();
}

@Override
public void load(InputStream iniConfig) {
// do nothing
}

@Override
public void load(Scanner scanner) {
//do nothing
}

/**
* Configure this ini.
*/
private void config() {
Section main = this.addSection("main");
main.put("authc.loginUrl", "/login");
main.put("authc.successUrl", "/");
main.put("authc.usernameParam", "user");
main.put("authc.passwordParam", "pass");
main.put("authc.rememberMeParam", "remember");

main.put("ds", com.mchange.v2.c3p0.ComboPooledDataSource.class.getCanonicalName());
...

main.put("matcher", org.apache.shiro.authc.credential.HashedCredentialsMatcher.class.getCanonicalName());
main.put("matcher.hashAlgorithmName", "SHA-256");

main.put("jdbcRealm", org.apache.shiro.realm.jdbc.JdbcRealm.class.getCanonicalName());
...

main.put("jdbcRealm.dataSource", "$ds");
main.put("jdbcRealm.credentialsMatcher", "$matcher");
main.put("securityManager.realms", "$jdbcRealm");

Section urls = this.addSection("urls");
urls.put("/VAADIN/**", "anon");
urls.put("/login/**", "anon");
urls.put("/login", "authc");
urls.put("/**", "authc");
}
}

关于java - 使用 Servlet 监听器和过滤器的 HTTP 服务的编程配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com