2017-05-02 7 views
1

私は、Springセキュリティでの例外処理についてのご質問があります。UserDetailsS​​erviceの例外処理

Iは例外をスローする必要があり、時には(SM_USERヘッダーの値がfalseの場合)CustomUserDetailsServiceを定義しています。

public class CustomUserDetailsService implements UserDetailsService { 

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

    ... throw new UsernameNotFoundException("wrong value of the sm Header");   
    ... 
} 

私は、この場合にスローされるHTTPステータスコード403(アクセス拒否)を好きだろうが、春のセキュリティは常に500を示しており、標準の(私のソフトウェアのための)例外表現を示すことができません。私は例外の解決者が私を助けることができると思った。しかし、私が標準のException Resolverを読んだ限り、SpringSecurityには適さず、そのスコープはSpring MVCだけです。

@ControllerAdvice 

パブリッククラスExceptionResolverはAbstractHandlerExceptionResolver {

@Override 
protected ModelAndView doResolveException(HttpServletRequest request, 
     HttpServletResponse responce, Object handler, Exception exception) { 

    ModelAndView toReturn = new ModelAndView(); 
    toReturn.setView(new MappingJackson2JsonView()); 
    toReturn.addObject("message", exception.getMessage()); 
    toReturn.addObject("exceptionClass", exception.getClass().getCanonicalName()); 

    HttpStatus exceptionStatus = getStatus(exception); 
    responce.setStatus(exceptionStatus.value());   
    return toReturn; 
} 

private HttpStatus getStatus(Exception exception){ 

    if (exception instanceof UsernameNotFoundException) 
     return HttpStatus.FORBIDDEN;   
    return HttpStatus.BAD_REQUEST; 
} 
} 

UserDetailsServiceの例外を解決する方法はあり拡張しますか?あなたが求めている何

UPDATE

15:42:37,948 DEBUG qtp1052330967-15 security.DelegateRequestMatchingFilter:137 - preAuthenticatedPrincipal = dhdg, trying to authenticate 
15:42:37,950 INFO qtp1052330967-15 security.CustomUserDetailsService:57 - looking for authorities for sm header value: [dhdg] 
15:42:37,989 DEBUG qtp1052330967-15 security.DelegateRequestMatchingFilter:225 - Cleared security context due to exception 
org.springframework.security.core.userdetails.UsernameNotFoundException: there is no opened sessions with this sm user header value 
    at de.escosautomation.restserver.security.CustomUserDetailsService.loadUserByUsername(CustomUserDetailsService.java:73) 
    at org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper.loadUserDetails(UserDetailsByNameServiceWrapper.java:53) 
    at org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.authenticate(PreAuthenticatedAuthenticationProvider.java:87) 
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:167) 
    at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doAuthenticate(AbstractPreAuthenticatedProcessingFilter.java:145) 
    at org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:113) 
    at de.escosautomation.restserver.security.DelegateRequestMatchingFilter.doFilter(DelegateRequestMatchingFilter.java:51) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) 
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) 
    at org.eclipse.jetty.server.Server.handle(Server.java:497) 
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310) 
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) 
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) 
    at java.lang.Thread.run(Thread.java:745) 
+0

その500のスタックトレースがありますか? – holmis83

+0

@ holmis83 500コードは、エラーのhttp表現で示されています。 Javaトレースは通常の例外のように見えます。 – user2957954

答えて

0

たちはHTTPエラーコードを変更したい場合に一般的に、我々は我々自身の 認証失敗ハンドラおよび認証を実装することでそれを行う、春のセキュリティ設定に関連していますあなたの他の疑い

について

<form-login 
    authentication-success-handler-ref="mySuccessHandler" 
    authentication-failure-handler-ref="myFailureHandler" 
    /> 

<beans:bean id="mySuccessHandler" 
    class="org.rest.security.MySavedRequestAwareAuthenticationSuccessHandler"/> 
    <beans:bean id="myFailureHandler" class= 
    "org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"/> 

:必要とし、それらの豆に怒鳴るようなものを指しているよう-successハンドラ

"私はExceptionリゾルバが私を助けてくれると思った。しかし、私が標準のException ResolverがSpringSecurityに適合していないため、そのスコープはSpring MVCだけです。 "

私たちのカスタムUserDetailsS​​ervice実装で例外を投げて認証チェーンを破棄し、 (例を言う)ユーザによって提供さが一致していません。

を私に教えてください、私は説明するために何かを逃した場合。私はこれが役立つことを願っていますし、そう

ハッピー符号化法!!!!!!!!!!!

+0

実際に私はSimpleUrlAuthenticationFailureHandlerの実現を持っていますが、動作しません。ログ行も出力されません。 – user2957954

1

私は01、あなたがPreAuthenticatedAuthenticationProviderを使用しているので、私は。ここに間違って起こっているかを見ると思いますは実際には処理されません。これは、典型的な事前認証環境では、ユーザーが存在することがすでに確認されているためです。

は、代わりにあなたはをロック 無効状態でUserDetailsオブジェクトを返す必要があります。

あなたはすべてのユーザー情報を読み取ることができない場合は、AuthenticationServiceExceptionを投げることができました。