我正在尝试将Apache Shiro集成到我的Spring Web MVC项目中,但是在适当的bean定义方面存在问题。这是我的 applicationContext.xml :
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/jdbc.properties</value>
<value>/WEB-INF/wmsauth.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.smth.smth.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="shiroFilter" class= "org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login.jsp"/>
<property name="successUrl" value="/login/index"/>
<property name="filterChainDefinitions">
<value>
/login.jsp = authc
/login/** = authc
/admin/** = authc
/logout.htm = logout
</value>
</property>
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="adminRealm"/>
<property name="cacheManager" ref="cacheManager"/>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<bean id="adminRealm" class="com.smth.smth.model.admin.AdminRealm" autowire="byName">
<property name="credentialsMatcher" ref = "sha256Matcher"/>
<property name="authenticationQuery" value = "SELECT password, salt FROM admin WHERE email = ?"/>
<property name="permissionsLookupEnabled" value = "true"/>
<property name="userRolesQuery" value = "SELECT role_name FROM admin_role WHERE email = ?"/>
<property name="permissionsQuery" value = "SELECT permission FROM roles_permission WHERE role_name = ?"/>
<property name="dataSource" ref = "dataSource"/>
</bean>
<bean id="sha256Matcher" class="org.apache.shiro.authc.credential.Sha256CredentialsMatcher" >
<property name="storedCredentialsHexEncoded" value = "false"/>
<property name="hashIterations" value = "1024"/>
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
</beans>
正如您所看到的,我在此处使用adminRealm
定义了autowire = "byName"
bean。
AdminRealm
课程如下:
package com.smth.smth.model.admin;
import com.smth.smth.service.AdminService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SaltedAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.springframework.beans.factory.annotation.Autowired;
public class AdminRealm extends JdbcRealm {
@Autowired
AdminService adminService;
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken userPassToken = (UsernamePasswordToken) token;
final String username = userPassToken.getUsername();
if (username == null) {
System.out.println("Username is null.");
return null;
}
final Admin admin = adminService.getByEmail(username);
if (admin == null) {
System.out.println("User does not exist with principal: [" + username + "]");
return null;
}
SaltedAuthenticationInfo info = new AdminAuthInfo(username, admin.getPassword(), admin.getSalt());
return info;
}
}
这里我们有AdminService
@Autowired
我希望通过 applicationContext.xml 中的配置通过Spring依赖注入来处理,但我一直得到java.lang.NullPointerException
1}}在adminService.getByEmail(username);
。任何信息都将非常感激。
我认为组件扫描不应该有任何问题。我的dispatcher-servlet
如下:
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.smth.smth"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
答案 0 :(得分:1)
你有一个adminService setter吗?如果没有,请添加#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Copyright 2014 by mimvp.com
def get_valid_ip(cls, ip_str):
ip_str_new = ''
ip_re = re.compile(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b')
ipList = ip_re.findall(ip_str)
if len(ipList) >= 1:
ip_str_new = ipList[0]
ip_str_new = ip_str_new.lstrip("0") # '05.9.87.163' ==> '5.9.87.163'
return ip_str_new
if __name__ == '__main__':
result = os.popen("/sbin/ifconfig en0 | grep broadcast")
ip_inet = result.read()
for ip_str in ip_inet.split(" "):
if self.get_valid_ip(ip_str) :
print ip_str
然后重试。
以下是参考: Spring Reference - Autowiring collaborators
例如,如果是 bean定义按名称设置为autowire,它包含一个master 属性(也就是说,它有一个setMaster(..)方法),Spring寻找一个 bean定义名为master,并使用它来设置属性。
答案 1 :(得分:0)
您的AdminService bean未在spring上下文中注册。尝试在context.xml中添加它或通过注释声明它。