2013-06-13 2 views
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; 
    } 
} 

출력은 그냥 : I 줄 아무것도하지 않는다 칭찬 할 때

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.springframew[email protected]9561: Principal: aaa; Credentials: [PROTECTED]; Authenticated: false; Details: org.sprin[email protected]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> 

답변

1

그것은이다 그 방법 simpleService.doNothing() 결코 따라서 스레드 문제가 발생할 원인, 반환, 보인다 이유에서 다른 코드 당신의 authenticate() 메소드에 도달하지 않았습니다.

그런데 디버거를 사용한 간단한 테스트로 문제를 쉽게 알 수 있습니다.

편집 : 디버깅 여기에 옵션을 선택하지 않습니다 이해 때문에 은 갈 수있는 방법은 방법 내부 및 방법 후 메서드를 호출하기 전에 로그 인쇄하는 것입니다.

당신이 그것을 한 적이과 방법이 인쇄되지 않기 때문에, 지금은 2 가지 옵션이 있습니다 :

  1. simpleReceiver가 null의 경우, 로그와 간단한 시도 캐치를 공개한다.

  2. simpleReceiver는 SimpleReceiver 유형이 아니지만 그 함수에 무한 길이의 작업 을 수행하는 파생 클래스입니다. 그렇다면 인스턴스 유형의 간단한 로그 인쇄에서이를 나타낼 수 있습니다.

+1

doNothing 정말 아무것도하지 않고 : 공공 무효 doNothing() 나는 방법이 로그인 – hudi

+0

흠 { // TODO 자동 생성 방법은 스텁}하지 않으면 당신은 코드를 주석 때 당신이 그것을 부르면 작동하지 않습니다. 디버거를 보셨습니까? – hudi

+0

를 디버깅하는 방법 아무 생각 –

관련 문제