我正在关注this tutorial以使用Spring Security。我想使用Hibernate进行数据库查询。我配置了代码,但抛出了异常。
我在spring-security.xml
和my-servlet.xml
中定义了bean,但仍然没有结果。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myMemberDetailsService' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320)
... 93 more
MemberRepository
public class MemberRepositoryImpl implements MemberRepository {
SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public Member findByUserName(String username) {
List<Member> members = new ArrayList<Member>();
members = sessionFactory.getCurrentSession()
.createQuery("from Member where username=?")
.setParameter(0, username).list();
if (members.size() > 0) {
return members.get(0);
} else {
return null;
}
}
}
MyMemberDetailsService
public class MyMemberDetailsService implements UserDetailsService {
private MemberRepository memberRep;
@Override
public UserDetails loadUserByUsername(final String username)
throws UsernameNotFoundException {
Member member = memberRep.findByUserName(username);
HashSet<String> roles = new HashSet<String>();
roles.add("ROLE_MEMBER");
List<GrantedAuthority> authorities = buildUserAuthority(roles);
return buildUserForAuthentication(member, authorities);
}
// Converts com.mkyong.users.model.User user to
// org.springframework.security.core.userdetails.User
private User buildUserForAuthentication(Member member,
List<GrantedAuthority> authorities) {
return new User(member.getUsername(), member.getPassword(),
member.isEnabled(), true, true, true, authorities);
}
private List<GrantedAuthority> buildUserAuthority(Set<String> userRoles) {
Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
// Build user's authorities
for (String userRole : userRoles) {
setAuths.add(new SimpleGrantedAuthority(userRole));
}
List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(
setAuths);
return Result;
}
}
我-servlet.xml中
<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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http:/www.springframework.org/schema/beans
http:/www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:/www.springframework.org/schema/aop
http:/www.springframework.org/schema/aop/spring-aop-3.0.xsd
http:/www.springframework.org/schema/tx
http:/www.springframework.org/schema/tx/spring-tx-3.0.xsd
http:/www.springframework.org/schema/context
http:/www.springframework.org/schema/mvc">
<context:annotation-config />
<mvc:annotation-driven />
<mvc:view-controller path="/index" />
<mvc:view-controller path="/" view-name="index" />
<mvc:view-controller path="/signin" />
<mvc:resources mapping="resources/**" location="resources/" />
<context:component-scan base-package="com.myProject" />
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="memberRep" class="com.myProject.repository.MemberRepositoryImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="myMemberDetailsService" class="com.myProject.service.MyMemberDetailsService">
<property name="memberRep" ref="memberRep" />
</bean>
<!-- Hibernate Config -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:8889/myProject" />
<property name="username" value="jack" />
<property name="password" value="jack" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
depends-on="dataSource">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.myProject.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="userServicePointCut"
expression="execution(* com.myProject.service.*Service.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="userServicePointCut" />
</aop:config>
<!-- End Hibernate Config -->
</beans>
弹簧security.xml文件
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<beans:import resource='login-service.xml' />
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/member**" access="hasRole('ROLE_MEMBER')" />
<intercept-url pattern="/signin" access="permitAll" />
<access-denied-handler error-page="/403" />
<form-login login-page="/signin" default-target-url="/index"
authentication-failure-url="/signin?error" username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf />
</http>
<authentication-manager>
<authentication-provider user-service-ref="myMemberDetailsService">
<password-encoder hash="bcrypt" />
</authentication-provider>
</authentication-manager>
</beans:beans>
答案 0 :(得分:4)
我刚刚阅读了教程,并且我看到它没有提及有关web.xml
配置的任何内容,所以我下载了它以了解它是如何配置的。
你说过,你曾尝试在myMemberDetailsService
和my-servlet
这两个地方定义spring-security
,但它没有用。
我认为这是一个配置问题所以请尝试更改您的web.xml
如下(我使用了您在问题中使用的名称,但是请检查一下,看看是否有出错了):
<servlet>
<servlet-name>my-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/my-servlet.xml,
/WEB-INF/spring-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
如果有<context-param>
参数,请删除全局contextConfigLocation
。
如果这不起作用,我建议你使用与教程类似的配置,就是说,将最小的MVC配置保留到my-servlet.xml
并在单独的上下文中配置其余的bean。
希望它有所帮助!
答案 1 :(得分:0)
在UserDetailsService
security
中加入xml
即可解决问题。
在spring-security.xml
中包含MyMemberDetailsService实现定义。
<beans:bean class="your.package.MyMemberDetailsService"
id="myMemberDetailsService" />
我发现您在login-service.xml
中加入了spring-security.xml
。因此,在login-service.xml
中定义用户服务bean也将解决问题。
答案 2 :(得分:0)
根据教程,web.xml应该包含对spring config xml的引用。
web.xml看起来像
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
mvc-dispatcher-servlet xml应该包含所有其他xmls的导入。
mvc-dispatcher-servlet 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"
...">
<import resource="my-servlet.xml" />
<import resource="spring-security.xml" />
...
</beans>