0

Webサービスを呼び出すことで既存のSpring Security認証を無効にする方法と、失敗した場合は、サードパーティのログインページをリダイレクトする必要があります。既存のSpring Security認証を無効にする

この認証Webサービスを呼び出すには、ServletRequestパラメータを取得する必要があります。リダイレクトするには、ServletResponseにアクセスする必要があります。

したがって、私はServletRequestとServletResponseパラメータでいくつかの認証方法を見つける必要があります。

しかし、私はそのようなProcessingFilterまたはAuthenticationProviderを見つけられませんでした。

Spring Securityの基本によると、AuthenticationProvider関連の認証メソッドをオーバーライドする必要があるようです。

ケースを使用するようによると、私は、春のセキュリティ事前認証を実装する必要が

しかし問題は、認証パラメータを持つPreAuthenticatedAuthenticationProvider関連の「認証」メソッドです。

PreAuthenticatedAuthenticationProvider

public class PreAuthenticatedAuthenticationProvider implements 
     AuthenticationProvider, InitializingBean, Ordered { 

    public Authentication authenticate(Authentication authentication) {} 

} 

ソリューションとして、AuthenticationFailureHandlerのカスタム実装を使用するための任意の可能性はありますか?

ありがとうございました。

私は次のようにして問題を解決してしまっている

答えて

0

オーバーライドのdoFilter方法

@Override 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 

    HttpServletRequest request = (HttpServletRequest) req; 
    HttpServletResponse response = (HttpServletResponse) res; 

    try { 

     // Get current Authentication object from SecurityContext 
     Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 

     // Call for third party WS when the Authenticator object is null 
     if (auth == null) { 

      logger.debug("doFilter : Proceed the authentication"); 

      String appId = "My_APP_ID"; 
      String redirectURL = request.getRequestURL().toString(); 

      // Call for third party WS for get authenticate 
      if (WS_Authenticator.isAuthenticated(appId, redirectURL)) { 

       // Successfully authenticated 
       logger.debug("doFilter : WS authentication success"); 

       // Get authenticated username 
       String userName = WS_Authenticator.getUserName();    

       // Put that username to request 
       request.setAttribute("userName", userName); 

      } else { 

       String redirectURL = WS_Authenticator.getAuthorizedURL(); 
       logger.debug("doFilter : WS authentication failed"); 
       logger.debug("doFilter : WS redirect URL : " + redirectURL); 

       ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
       ((HttpServletResponse) response).sendRedirect(redirectURL); 

       // Return for bypass the filter chain 
       return; 
      } 

     } else { 
      logger.debug("doFilter : Already authenticated"); 
     } 

    } catch (Exception e) { 
     logger.error("doFilter: " + e.getMessage());    
    } 

    super.doFilter(request, response, chain); 
    return; 
} 

オーバーライドAbstractPreAuthenticatedProcessingFilterカスタムの実装が満たさgetPreAuthenticatedCredentials

上書きloadUserDetails方法CustomAuthenticationUserDetailsS​​erviceImpl実装

@Override 
protected Object getPreAuthenticatedCredentials(HttpServletRequest request) { 

    // Get authenticated username 
    String[] credentials = new String[1]; 
    credentials[0] = (String) request.getAttribute("userName"); 

    return credentials; 
} 
  • HOD

    public class CustomAuthenticationUserDetailsServiceImpl implements AuthenticationUserDetailsService<Authentication> { 
    
        protected static final Logger logger = Logger.getLogger(CustomAuthenticationUserDetailsServiceImpl.class); 
    
        @Autowired 
        private UserDataService userDataService; 
    
        public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException { 
    
         // Get authenticated username 
         String[] credentials = (String[]) token.getCredentials(); 
         String userName = credentials[0]; 
    
         try { 
    
          // Get user by username 
          User user = userDataService.getDetailsByUserName(userName); 
    
          // Get authorities username    
          List<String> roles = userDataService.getRolesByUserName(userName);   
          user.setCustomerAuthorities(roles); 
          return user; 
    
         } catch (Exception e) { 
          logger.debug("loadUserDetails: User not found! " + e.getMessage()); 
          return null; 
         }  
        } 
    } 
    
関連する問題