1

カスタム認証プロバイダーがあります。他のクラスからメソッドを呼び出したい場合、何も起こらず、理由がわかりません。助けてください:

package org.sample.web.security;

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

import org.sample.web.service.SimpleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

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

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        logger.error("1111111111111111");
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();
        simpleService.doNothing();
        logger.error("2222222222222222");

        if (name.equals("admin") && password.equals("system")) {
            List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
            Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
            return auth;
        } else {
            return null;
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }

    public void setSimpleService(SimpleService simpleService) {
        this.simpleService = simpleService;
    }
}

認証方法の出力は次のとおりです。

DEBUG - ProviderManager            - Authentication attempt using org.sample.web.security.CustomAuthenticationProvider
ERROR - stomAuthenticationProvider - 1111111111111111

私がラインをほめたときは何もしない

//            simpleService.doNothing();

それからそれはなぜ働き始めるのですか?

DEBUG - ProviderManager            - Authentication attempt using org.sample.web.security.CustomAuthenticationProvider
ERROR - stomAuthenticationProvider - 1111111111111111
ERROR - stomAuthenticationProvider - 2222222222222222
TRACE - XmlWebApplicationContext   - Publishing event in Root WebApplicationContext: org.springframework.security.authentication.event.AuthenticationFailureProviderNotFoundEvent[source=org.springframework.security.authentication.UsernamePasswordAuthenticationToken@9561: Principal: aaa; Credentials: [PROTECTED]; Authenticated: false; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Not granted any authorities]
DEBUG - DefaultListableBeanFactory - Returning cached instance of singleton bean 'methodRegistrar'
DEBUG - BasicAuthenticationFilter  - Authentication request for failed: org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken
DEBUG - nSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
DEBUG - tyContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed

セキュリティ コンテキスト:

<?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 use-expressions="true">
        <intercept-url pattern="/**" access="isAuthenticated()" />
        <http-basic />
    </http>

    <global-method-security pre-post-annotations="enabled" />

    <authentication-manager>
        <authentication-provider ref="customAuthenticationProvider" />
    </authentication-manager>

</beans:beans>
4

1 に答える 1

1

あなたの説明から、メソッド simpleService.doNothing() が返されないように思われるため、スレッドが動かなくなります。これが、authenticate() メソッドの他のコードに到達しない理由です。

ところで、デバッガーで簡単なテストを行うと、問題が簡単に明らかになるはずです。

編集: デバッグはここではオプションではないことを理解しているので、方法は、メソッドを呼び出す前、メソッド内、およびメソッドの後にログに出力することです。

実行してもメソッドが印刷されないため、次の 2 つのオプションがあります。

  1. simpleReceiver は null です。ログを使用した単純な try catch で明らかになります。

  2. simpleReceiver は SimpleReceiver 型ではありませんが、その関数で無限に長い作業を行う派生クラスです。その場合、インスタンス型の単純なログ出力で明らかになります。

于 2013-06-13T10:46:48.990 に答える