gpt4 book ai didi

java - Shiro 1.2 不能与 Guice(和 Vaadin)一起工作

转载 作者:行者123 更新时间:2023-11-29 09:16:41 25 4
gpt4 key购买 nike

初次使用,请多关照!

我在配置 Shiro 以使用 Guice 过滤 Vaadin 生成的页面时遇到了一些问题。

我在各种网站上在线查看,包括 Apache Shiro 的指南等。问题是大多数网站倾向于使用“旧”时尚方式,即使用 Shiro 1.1(没有原生 Guice 支持)。

所以问题来了。我的页面不会被 Shiro 过滤。我尝试了无数种不同的方法,包括使用 AOP 进行方法身份验证,在 web.xml 中手动设置过滤器。甚至设置一个 shiro.ini 文件(我在任何情况下都不想这样做)。

所以这是我正在使用的东西的列表:- Shiro 1.2.0-快照- 指南 3.0-Vaadin 6.7.4

这是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>App</display-name>

<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>

<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.app.GuiceServletInjector</listener-class>
</listener>

</web-app>

这是 Servlet 注入(inject)器:

public class GuiceServletInjector extends GuiceServletContextListener {
private ServletContext servletContext;

@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContext = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}

@Override
protected Injector getInjector() {
return Guice.createInjector(new GuiceServletModule(), new ShiroConfigurationModule(servletContext));
}

然后创建一个 ServletModule,它将请求传递给 Vaadin 应用程序:

protected void configureServlets() {
bind(Application.class).to(VaadinMainWindow.class).in(ServletScopes.SESSION);

bind(BasicHttpAuthenticationFilter.class).in(Singleton.class);
filter("/*").through(BasicHttpAuthenticationFilter.class);

serve("/*", "*").with(VaadinApp.class);
}

同样在注入(inject)器阶段,请注意我创建了一个 ShiroConfigurationModule,它负责领域等:

public class ShiroConfigurationModule extends ShiroWebModule {

@Inject
public ShiroConfigurationModule(ServletContext servletContext) {
super(servletContext);
}

@Override
protected void configureShiroWeb() {
bindRealm().to(ShiroBaseRealm.class).in(Singleton.class);
bind(Realm.class).to(ShiroBaseRealm.class).in(Singleton.class);

processMethodInterceptors();
}

private void processMethodInterceptors() {
MethodInterceptor interceptor = new AopAllianceAnnotationsAuthorizingMethodInterceptor();
bindInterceptor(any(), annotatedWith(RequiresRoles.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresPermissions.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresAuthentication.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresUser.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresGuest.class), interceptor);
}
}

realm 类为 supports() 返回“true”,但为所有内容返回“null”,模拟用户不存在。

做错事或漏掉一步的可能性非常高。有人可以解释一下我遗漏了什么,这样我至少可以获得基本的 HTTP 身份验证吗?

非常感谢!莫.

最佳答案

自然地,博客链接已经过期,现在重定向到的站点不包含该博客文章的任何痕迹。

可以在此处找到该文章的副本。

http://web.archive.org/web/20120413052117/http://www.mofirouz.com/wordpress/2012/01/guice-shiro-1-2-and-vaadingwt/

答案的症结所在:如果您使用的是 guice,那么您必须包含

filter("/*").through(GuiceShiroFilter.class)

在你的 ServletModule 中,否则相关的 shiro 过滤器都不会被命中。

关于java - Shiro 1.2 不能与 Guice(和 Vaadin)一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8952463/

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