shiro+ssm启动报错

求知a 发布于 01/16 10:37
阅读 82
收藏 0

第一次使用shiro,遇到好多的问题。项目启动就报错,求大神帮忙看看

报错信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter': FactoryBean threw exception on object creation; nested exception is org.apache.shiro.config.ConfigurationException: Unable to parse filter chain definition token: 
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:173)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1467)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
	at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:323)
	at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235)
	at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
	at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:926)
	at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:889)
	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1149)
	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
	at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
	at org.apache.catalina.core.StandardService.start(StandardService.java:516)
	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.apache.shiro.config.ConfigurationException: Unable to parse filter chain definition token: 
	at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.toNameConfigPair(DefaultFilterChainManager.java:238)
	at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.createChain(DefaultFilterChainManager.java:145)
	at org.apache.shiro.spring.web.ShiroFilterFactoryBean.createFilterChainManager(ShiroFilterFactoryBean.java:397)
	at org.apache.shiro.spring.web.ShiroFilterFactoryBean.createInstance(ShiroFilterFactoryBean.java:437)
	at org.apache.shiro.spring.web.ShiroFilterFactoryBean.getObject(ShiroFilterFactoryBean.java:343)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:166)
	... 35 more
Caused by: java.lang.IllegalArgumentException: Filter name not found for filter chain definition token: 
	at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.toNameConfigPair(DefaultFilterChainManager.java:204)
	... 40 more

 

Web.xml

<display-name></display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:applicationContext-*.xml,
    classpath:spring-shiro.xml
    </param-value>
  </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>
	      org.springframework.web.filter.CharacterEncodingFilter
	  </filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- shiro过滤器 -->
  <filter>
		<filter-name>shiroFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>shiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
      classpath:springmvc-servlet.xml,
    classpath:spring-shiro.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpeg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.mp4</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.eot</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.svg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.ttf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.woff</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.woff2</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.otf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.psd</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.xls</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.pdf</url-pattern>
  </servlet-mapping>


  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>SMBMS_C12_12.root</param-value>
  </context-param>
  <listener>
    <listener-class>
			org.springframework.web.util.Log4jConfigListener
	</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/back/customerprice.jsp</welcome-file>
  </welcome-file-list>

 

spring-shiro.xml

<bean id="myShiroRealm" class="com.rn.Filter.UserRealm"></bean>
	
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="myShiroRealm"></property>
		<property name="cacheManager" ref="cacheManager" />		
	</bean>
	
	<!-- 用户授权信息Cache -->
	<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
	
	
	<bean id="shiroFilter" class="com.rn.Filter.MyShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager"></property>
		<property name="loginUrl" value="/manager/login"></property>
		<property name="successUrl" value="/test/homepage"></property>
		<property name="unauthorizedUrl" value="/test/error"></property>
		<property name="filterChainDefinitions">
			<value>
				/test/login=anon,
				<!-- /test/*=authc -->
			</value>
		</property>
		<property name="filters">
			<map>
				<entry key="roles">
					<bean class="com.rn.Filter.MyShiroFilter"/>
				</entry>
			</map>
		</property>
	</bean>
	
<!-- 配置Shiro在Spring中的生命周期的控制操作 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
springmvc-servlet.xml

<mvc:annotation-driven>
	 	<mvc:message-converters>
	 		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
	 			<property name="supportedMediaTypes">
	 				<list>
	 					<value>application/json;charset=UTF-8</value>
	 				</list>
	 			</property>
	 		</bean> 
	 		<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
	 			<property name="supportedMediaTypes">
	 				<list>
	 					<value>text/html;charset=UTF-8</value>
	 					<value>application/json</value>
	 				</list>
	 			</property>
	 			<property name="features">
	 				<list>
	 				 <!--   Date的日期转换器 -->
					  <value>WriteDateUseDateFormat</value>
					</list>
				</property>
			</bean>
			  <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
                <property name="objectMapper" ref="customObjectMapper"></property>  
            </bean>  
		</mvc:message-converters>
	</mvc:annotation-driven>
	
	<context:component-scan base-package="com.rn.controller,com.rn.dao,com.rn.service,com.rn.service.impl,com.rn.Filter">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<bean id="customObjectMapper" class="com.rn.demo.CustomObjectMapper"></bean> 
	
	<mvc:resources location="/statics/" mapping="/statics/**"></mvc:resources>
	
	<!-- 配置多视图解析器:允许同样的内容数据呈现不同的view -->
	
				<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
					<property name="prefix" value="/WEB-INF/jsp/"/>
					<property name="suffix" value=".jsp"/>
				</bean>		
			
	  

	<!-- <mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/user/sys/**"/>
			<bean class="cn.smbms.interceptor.SysInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors> -->
	
	
	<!-- 配置MultipartResolver,用于上传文件,使用spring的CommonsMultipartResolver -->  
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		 <property name="maxUploadSize" value="5000000"/>
		 <property name="defaultEncoding" value="UTF-8"/>
	</bean>
	
	<!-- 配置启用Shiro的注解功能 -->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" 
		depends-on="lifecycleBeanPostProcessor">
		<property name="proxyTargetClass" value="true"></property>
	</bean>
		
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
	    <property name="securityManager" ref="securityManager"/>
	</bean>

 

UserRealm.java

public class UserRealm extends AuthorizingRealm {

	@Autowired
	private UserService userSer;

	/**
	 * 每次验证权限执行
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principal) {
		
		System.out.println("权限验证");
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User) session.getAttribute(Constants.SESSION_USER);
		List<String> roleIds = userSer.getUserRoleIds(user.getUser_Code());
		if (roleIds != null && roleIds.size() > 0) {
			SimpleAuthorizationInfo info= new SimpleAuthorizationInfo();
			for (String roleId : roleIds) {
				info.addRole(roleId);
			}
			return info;
		}
		return null;
	}

	/**
	 * 每次登陆验证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authenticationToken)
			throws AuthenticationException {
		String user_Name = (String) authenticationToken.getPrincipal();
		User user = userSer.getpassword(user_Name);
		if (user != null) {
			return new SimpleAuthenticationInfo(user_Name, user.getPassword(),
					null, getName());
		}
		return null;
	}

}

 

MyShiroFilterFactoryBean.java

public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean {

	private static final String ROLE_STRING = "roles[{0}]";
	private String filterChainDefinitions;

	@Autowired
	private RolePermissionService RPService;
	
	@Override
	public void setFilterChainDefinitions(String definitions) {
		filterChainDefinitions = definitions;
		Ini ini = new Ini();
		ini.load(definitions);
		Ini.Section section = ini.getSection("urls");
		if (CollectionUtils.isEmpty(section)) {
			section = ini.getSection("");
		}

		List<MenuPermission> menuPermissions=RPService.getMenuPerms();
		if (menuPermissions!=null) {
			for (MenuPermission menuPermission : menuPermissions) {
				List<String> group_code=menuPermission.getGroup_code();
				if (StringUtils.hasLength(menuPermission.getUrl())&&group_code!=null&&group_code.size()>0) {
					StringBuilder sb = new StringBuilder();
					for (String role : group_code) {
						sb.append(role).append(",");
					}
					String str = sb.substring(0, sb.length() - 1);
					section.put(menuPermission.getUrl(), MessageFormat.format(ROLE_STRING, str));
				}
				
			}
		}
		section.put("/**", "authc");
		this.setFilterChainDefinitionMap(section);
	}

	public void update() {
		synchronized (this) {
			try {
				AbstractShiroFilter shiroFilter = (AbstractShiroFilter) this
						.getObject();
				PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) shiroFilter
						.getFilterChainResolver();
				DefaultFilterChainManager manager = (DefaultFilterChainManager) resolver
						.getFilterChainManager();
				manager.getFilterChains().clear();
				this.getFilterChainDefinitionMap().clear();

				this.setFilterChainDefinitions(filterChainDefinitions);

				Map<String, String> chains = this.getFilterChainDefinitionMap();
				if (!CollectionUtils.isEmpty(chains)) {
					Iterator var12 = chains.entrySet().iterator();
					while (var12.hasNext()) {
						Map.Entry<String, String> entry = (Map.Entry) var12
								.next();
						String url = (String) entry.getKey();
						String chainDefinition = (String) entry.getValue();
						manager.createChain(url, chainDefinition);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

 

MyShiroFilter.java

public class MyShiroFilter extends AuthorizationFilter {

	
	/**
	 * 判断用户是否可以放行
	 */
	@Override
	protected boolean isAccessAllowed(ServletRequest servletRequest,
			ServletResponse servletresponse, Object object) throws Exception {
		Subject subject=getSubject(servletRequest, servletresponse);
		String[] roles=(String[])object;
		if (roles==null||roles.length==0) {
			return true;
		}
		for (String role : roles) {
			if (subject.hasRole(role)) {
				return true;
			}
		}
		return false;
	}

	
	

}

麻烦各位大神帮忙看一下是怎么回事,这个问题已经折磨我好久了,一直解决不掉,拜托大神,真的很急

加载中
返回顶部
顶部