Spring 自定义UserDetails服务未正确执行

Spring 自定义UserDetails服务未正确执行,spring,hibernate,spring-mvc,controller,spring-security,Spring,Hibernate,Spring Mvc,Controller,Spring Security,我正在尝试配置Spring安全性,但我的自定义UserDetailsService有一个额外的问题。每次尝试登录时,我都会被重定向到失败页面。这是服务的代码: @Service("customUserDetailsService") public class CustomUserDetailsService implements UserDetailsService { private static final Logger logger = LoggerFactory.getLogg

我正在尝试配置Spring安全性,但我的自定义UserDetailsService有一个额外的问题。每次尝试登录时,我都会被重定向到失败页面。这是服务的代码:

@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    private static final Logger logger = LoggerFactory.getLogger(CustomUserDetailsService.class);

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {

        logger.info("This is printed");

        UserDetails userDetails = userService.findOne(username);

        logger.info("This isn't printed");

        if (userDetails == null) {
            logger.info("This isn't printed");
            throw new UsernameNotFoundException("No user found with username "
                    + username);
        }

        return userDetails;
    }

}
正如您在代码中所看到的,一些记录器从未打印过,这有点奇怪

以下是我的用户模型、存储库及其服务类:

@Entity
public class User implements UserDetails {
    // more code


@Transactional(readOnly = true)
public interface UserRepository extends JpaRepository<User, Long> {

    User findByUsername(String username);
}

@Repository
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public User findOne(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    @Transactional
    public User save(User user) {
        return userRepository.save(user);
    }
}
@实体
公共类用户实现UserDetails{
//更多代码
@事务(只读=真)
公共接口用户存储库扩展了JpaRepository{
用户findByUsername(字符串用户名);
}
@存储库
@事务(只读=真)
公共类UserServiceImpl实现UserService{
@自动连线
私有用户存储库用户存储库;
@凌驾
公共列表findAll(){
返回userRepository.findAll();
}
@凌驾
公共用户findOne(字符串用户名){
返回userRepository.findByUsername(用户名);
}
@凌驾
@交易的
公共用户保存(用户){
返回userRepository.save(用户);
}
}
My web.xml

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

    <!-- Spring Security filters -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- This is used to inject webroot property (used by persistence.xml) -->
    <listener>
        <listener-class>com.leakedbits.twicker.listener.WebAppPropertiesListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/security.xml
            /WEB-INF/spring/root-context.xml
        </param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
com.leakedbits.twicker.listener.WebAppPropertiesListener
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/spring/security.xml
/WEB-INF/spring/root-context.xml
org.springframework.web.context.ContextLoaderListener
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/appServlet/servlet-context.xml
1.
appServlet
/
My security.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/security 
        http://www.springframework.org/schema/security/spring-security-3.1.xsd
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <http pattern="/resources" security="none" />

    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/denied" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/user" access="hasRole('ROLE_USER')" />
        <intercept-url pattern="/init2" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')" />

        <form-login login-page="/login" authentication-failure-url="/login/failure"
            default-target-url="/" />

        <access-denied-handler error-page="/denied" />

        <logout invalidate-session="true" logout-success-url="/logout/success"
            logout-url="/logout" />
    </http>

    <!-- This line must to be here because for some random reason the implementation 
        wasn't found without it -->
    <beans:bean id="customUserDetailsService"
        class="com.leakedbits.twicker.service.security.CustomUserDetailsService" />

    <authentication-manager>
        <authentication-provider user-service-ref="customUserDetailsService">
            <password-encoder hash="sha" />
        </authentication-provider>
    </authentication-manager>
</beans:beans>

最后,我前面运行的添加测试用户的代码:

User user = new User();
user.setPassword("7c4a8d09ca3762af61e59520943dc26494f8941b"); // 123456
user.setUsername("davidmogar");
Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
user.setGrantedAuthorities(grantedAuthorities);
userService.save(user);
User User=新用户();
user.setPassword(“7c4a8d09ca3762af61e59520943dc26494f8941b”);/123456
user.setUsername(“davidmogar”);
Set grantedAuthories=new HashSet();
添加(新的SimpleGrantedAuthority(“角色管理”);
user.setGrantedAuthories(GrantedAuthories);
userService.save(用户);
我做错了什么

编辑

由于未知原因,userService为空,因此未自动连接。我不确定是哪一个问题。此服务在控制器内运行良好。

autowire=“byType”
添加到userDetailsService bean的xlm定义中


并从类中删除服务注释,否则将有两个实例。

我没有UserDetails服务bean定义。关于服务注释,我使用它加载服务,但由于未知原因,它无法工作,我必须添加bean定义。当我删除bean声明时,我得到以下错误:“未定义名为‘customUserDetailsService’的bean”我如何解决此问题?它在您的安全性中。XML!尝试创建一个将用户服务作为参数的构造函数。除了将@Auto-wire添加到此构造函数之外,请删除所有其他注释。在XML中使用Auto-wire,=”构造函数"如果您使用@Service etc,则需要启用组件扫描,否则Spring将无法定位和实例化该bean。请将其视为使用定义的快捷方式。@David Moreno Garcia:Jukka的解释是正确的。通常,通过组件扫描找到的bean由其none argumemt构造函数固定;如果您希望t要使用其他构造函数,请在此构造函数处放置
@Autowire
注释。