ホームページ  >  記事  >  Spring Securityは、残りのサービスで認証済みユーザーと未認証ユーザーのユーザー情報を取得します

Spring Securityは、残りのサービスで認証済みユーザーと未認証ユーザーのユーザー情報を取得します

王林
王林転載
2024-02-08 23:00:23690ブラウズ

Web アプリケーションを開発する場合、セキュリティは重要な考慮事項です。ユーザーデータを保護し、不正アクセスを防止するには、信頼できる認証および認可メカニズムを使用する必要があります。 Spring Security は、アプリケーションを保護するための完全なソリューション セットを提供する、強力で広く使用されているセキュリティ フレームワークです。この記事では、Spring Security で認証済みユーザーと未認証ユーザーのユーザー情報を取得する方法を説明します。 PHP エディタ Baicao では、Spring Security の機能を使用してユーザー情報を取得し、異なるサービス間でユーザー情報を共有する方法を説明します。初心者でも経験豊富な開発者でも、この記事は Spring Security に関する詳細情報を提供し、アプリケーションのセキュリティを向上させるのに役立ちます。

質問内容

Spring Rest サービスを持っており、認証済みユーザーと非認証ユーザーの両方に使用したいと考えています。ユーザーが認証されている場合は、securitycontextholder.getcontext().getauthentication()からユーザー情報を取得したいと思います。

  • 使用する場合 .antmatchers("/app/rest/question/useroperation/list/**").permitall() 以下に示すように ouath2 設定では、ユーザー情報を取得できます ユーザーは認証されていますが、認証されていないユーザーには 401 エラーが発生します。
  • ###もし私が
  • .antmatchers("/app/rest/question/useroperation/list/**").permitall() WebセキュリティでURLを無視します web.ignoring()..antmatchers("/app/rest/question/useroperation/list/**") 以下に示す securityconfiguration では、すべてのユーザーが電話をかけることができます。 サービスを利用しましたが、securitycontext からユーザー情報を取得できません。
認証済みユーザーと未認証ユーザーの URL を呼び出し、ユーザーのログイン時に securitycontext からユーザー情報を取得するように Spring セキュリティを構成するにはどうすればよいですか。

リーリー

セキュリティ構成

@configuration
@enableresourceserver
protected static class resourceserverconfiguration extends resourceserverconfigureradapter {

    @inject
    private http401unauthorizedentrypoint authenticationentrypoint;

    @inject
    private ajaxlogoutsuccesshandler ajaxlogoutsuccesshandler;

    @override
    public void configure(httpsecurity http) throws exception {
        http
                .exceptionhandling()
                .authenticationentrypoint(authenticationentrypoint)
                .and()
                .logout()
                .logouturl("/app/logout")
                .logoutsuccesshandler(ajaxlogoutsuccesshandler)
                .and()
                .csrf()
                .requirecsrfprotectionmatcher(new antpathrequestmatcher("/oauth/authorize"))
                .disable()
                .headers()
                .frameoptions().disable()
                .sessionmanagement()
                .sessioncreationpolicy(sessioncreationpolicy.stateless)
                .and()
                .authorizerequests()
                .antmatchers("/views/**").permitall()
                .antmatchers("/app/rest/authenticate").permitall()
                .antmatchers("/app/rest/register").permitall()
                .antmatchers("/app/rest/question/useroperation/list/**").permitall()
                .antmatchers("/app/rest/question/useroperation/comment/**").authenticated()
                .antmatchers("/app/rest/question/useroperation/answer/**").authenticated()
                .antmatchers("/app/rest/question/definition/**").hasanyauthority(authoritiesconstants.admin)
                .antmatchers("/app/rest/logs/**").hasanyauthority(authoritiesconstants.admin)
                .antmatchers("/app/**").authenticated()
                .antmatchers("/websocket/tracker").hasauthority(authoritiesconstants.admin)
                .antmatchers("/websocket/**").permitall()
                .antmatchers("/metrics/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/health/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/trace/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/dump/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/shutdown/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/beans/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/info/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/autoconfig/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/env/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/trace/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/api-docs/**").hasauthority(authoritiesconstants.admin)
                .antmatchers("/protected/**").authenticated();

    }

}

回避策

permitall() authentication オブジェクトが securitycontext に存在する必要があります。

非 OAuth ユーザーの場合、匿名アクセスを有効にすることでこれを実現できます:

リーリー

匿名アクセスでは、

securitycontext に ## がない場合に備えて、追加のフィルター anonymousauthenticationfilter がフィルター チェーンに追加され、認証情報として anonymousauthenticationtoken が設定されます。 #authenticationObject

/public/authphpcnendcphp 中国語:

経由で authuser をチェックするためのセキュリティ構成があります。 リーリー

以上がSpring Securityは、残りのサービスで認証済みユーザーと未認証ユーザーのユーザー情報を取得しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はstackoverflow.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。