亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用額外回調處理自定義登錄

使用額外回調處理自定義登錄

守著星空守著你 2023-04-19 15:05:39
不幸的是,您的解決方案沒有奏效。再次拋出同樣的錯誤。但是我最終能夠確定問題所在。問題出在我的 Wicket Application 類的構造函數中。在這個構造函數中,我調用了 super.init()。刪除此應用程序后啟動時沒有 Bean 實例化錯誤。public class WicketApplication extends AuthenticatedWebApplication {//  This caused the problem with instatiating the FilterRegistrationBean    //  public WicketApplication() {//      super.init();//  }    @Override    protected void init() {        super.init();        getComponentInstantiationListeners().add(new SpringComponentInjector(this));                mountPage("/admin", AdminPage.class);        mountPage("/login", LoginPage.class);    }    @Override    public Class<? extends Page> getHomePage() {        return AdminPage.class;    }    @Override    protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {        return AppAuthenticatedWebSession.class;    }    @Override    protected Class<? extends WebPage> getSignInPageClass() {        return LoginPage.class;    }    public static WicketApplication get() {        return (WicketApplication) Application.get();    }}我正在使用 Spring Security 5 和 Spring Boot 2.1 構建 OAuth2 提供程序服務器。就我而言,我的服務器必須與某些外部服務器通信以驗證用戶身份。這個外部服務器生活在恐龍時代,因此不使用像 OAuth 這樣的通用身份驗證機制。所以我必須劫持登錄請求,重定向到 dinosaur 服務器,手動處理該身份驗證(不幸的是,包括回調),然后返回到 spring security 以批準登錄請求并確保用戶獲得訪問令牌。劫持登錄請求如下:@Override  protected void configure ( HttpSecurity http ) throws Exception {    http        .requestMatchers()        .antMatchers( "/login", "/oauth/authorize", "/manuallogin" )        .and()        .authorizeRequests()        .anyRequest()        .authenticated()        .and()        .formLogin()        .loginPage( "/manuallogin" )        .permitAll()        .and().csrf().disable();如您所見,我需要接受另一個回調,所以我丟失了原始登錄請求,我無法發送響應。我想出了以下解決方案,通過調用 OAuth2 客戶端的回調 URL 來縮短。然而,這不起作用,因為 spring 不接受身份驗證。我必須以某種方式繼續原始登錄請求并對用戶進行身份驗證。即使您閱讀了所有這些,也非常感謝您:)
查看完整描述

1 回答

?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

對于遇到此問題的任何絕望的靈魂,這里是解決方案:


@RestController

public class MainLoginController {


  @RequestMapping("/manuallogin")

  ResponseEntity<Object> interceptLoginRequest ( ){

    ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

    DefaultSavedRequest springSecuritySavedRequest = (DefaultSavedRequest) requestAttributes.getRequest()

                                                                                            .getSession()

                                                                                            .getAttribute( "SPRING_SECURITY_SAVED_REQUEST" );

    queryString = springSecuritySavedRequest.getQueryString();

    request.getSession().setAttribute( "queryString", queryString );


    return ResponseEntity.status( HttpStatus.FOUND )

                         .location( URI.create( dinosaurServer.getLoginUrl() ) )

                         .build();

  }


  @RequestMapping("/handshakeWithDinosaur")

  public ResponseEntity<Object> handshakeWithDinosaur ( String dinosaursToken ) {



    Authentication authentication = this.authenticationManager.authenticate(

        new UsernamePasswordAuthenticationToken(

            dino.getUser(), dino.getPass()

        )

    );

    SecurityContext sc = SecurityContextHolder.getContext();

    sc.setAuthentication( authentication );

    request.getSession().setAttribute( SPRING_SECURITY_CONTEXT_KEY, sc );



    String queryString = String.valueOf( request.getSession().getAttribute( "queryString" ) );


    return ResponseEntity.status( HttpStatus.FOUND )

                         .location( URI.create( String.format( "%s?%s",SPRING_AUTH_ENDPOINT, queryString ) ) )

                         .build();

  }



@Component

public class AuthProviderForDinosaur implements AuthenticationProvider {



  @Override

  public Authentication authenticate ( Authentication authentication ) throws AuthenticationException {

    List<GrantedAuthority> grantedAuths = new ArrayList<>();

    grantedAuths.add( new SimpleGrantedAuthority( "ROLE_USER" ) );

    return new UsernamePasswordAuthenticationToken( authentication.getName(), authentication.getCredentials(), grantedAuths );

  }


  @Override

  public boolean supports ( Class<? extends Object> authentication ) {

    return ( UsernamePasswordAuthenticationToken.class.isAssignableFrom( authentication ) );

  }

}

基本上,我啟用了會話并讓 Spring 在會話中為我保存請求,同時服務器與恐龍服務器對話并完成握手。完成后,向 Spring 詢問先前請求的參數以通過 Spring Security 繼續授權。


查看完整回答
反對 回復 2023-04-19
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號