0

春のセキュリティで簡単なアプリケーションを作成しています。今、ログインに問題があります。最初のステップでは、パスワード テストでユーザー テストを受け入れたいので、次のようなものを作成しました。

<?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="false" use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
        <intercept-url pattern="/login.jsp" access="permitAll" />
        <intercept-url pattern="/logout.jsp" access="permitAll" />
        <intercept-url pattern="/**" access="isAuthenticated()" />

        <access-denied-handler error-page="/denied.jsp" />
        <logout invalidate-session="true" logout-success-url="/logout/success" logout-url="/logout" />

        <custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER" />
    </http>

    <beans:bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:property name="loginFormUrl" value="/login.jsp" />
    </beans:bean>

    <beans:bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
        <beans:property name="authenticationFailureHandler" ref="securityLoginFailureHandler" />
        <beans:property name="authenticationSuccessHandler" ref="securityLoginSuccessHandler" />
    </beans:bean>

    <beans:bean id="securityLoginSuccessHandler" class="org.sample.web.security.SecurityLoginSuccessHandler">
        <beans:property name="defaultTargetUrl" value="/login.jsp" />
    </beans:bean>

    <beans:bean id="securityLoginFailureHandler" class="org.sample.web.security.SecurityLoginFailureHandler">
        <beans:property name="defaultFailureUrl" value="/login/failure" />
    </beans:bean>

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

および customUserDeteialsService:

package org.sample.web.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

    @Service
    public class CustomUserDetailsService implements UserDetailsService {

        protected final Logger logger = LoggerFactory.getLogger(getClass());

        public UserDetails loadUserByUsername(String username) {
            logger.error("niceeeeeeeeeeeee");
            return new User("test", "test", getAuthorities(1));
        }

        /**
         * Retrieves a collection of {@link GrantedAuthority} based on a numerical role
         * 
         * @param role
         *            the numerical role
         * @return a collection of {@link GrantedAuthority

         */
        public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
            List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
            return authList;
        }

        /**
         * Converts a numerical role to an equivalent list of roles
         * 
         * @param role
         *            the numerical role
         * @return list of roles as as a list of {@link String}
         */
        public List<String> getRoles(Integer role) {
            List<String> roles = new ArrayList<String>();

            if (role.intValue() == 1) {
                roles.add("ROLE_USER");
                roles.add("ROLE_ADMIN");

            } else if (role.intValue() == 2) {
                roles.add("ROLE_USER");
            }

            return roles;
        }

        /**
         * Wraps {@link String} roles to {@link SimpleGrantedAuthority} objects
         * 
         * @param roles
         *            {@link String} of roles
         * @return list of granted authorities
         */
        public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) {
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            for (String role : roles) {
                authorities.add(new SimpleGrantedAuthority(role));
            }
            return authorities;
        }

    }

問題は、認証がまだ失敗していることです。これをデバッグすると、次のことがわかります。

ERROR - CustomUserDetailsService   - niceeeeeeeeeeeee
DEBUG - DaoAuthenticationProvider  - Authentication failed: password does not match stored value

私が間違っていることは何ですか?

4

1 に答える 1

0

あなたの構成でわかり<password-encoder hash="md5" />ます。md5Authentication-Provider でパスワードをエンコードするために使用していることを意味します。したがって、あなたCustomUserDetailsServiceもmd5 を使用して「テスト」をエンコードする必要があります。

new User("test", "encoded_password", getAuthorities(1));
于 2013-06-10T15:47:05.423 に答える