新しい権限を作成する方法 パーマリンク to "新しい権限を作成する方法"

このTipは@Tonteriasにより提出されました

ADMINとUSERの指定された権限に加えて、新しい権限が必要だとします。新しい権限をROLE_EXAMPLE_AUTHORITYとします。

AuthoritiesConstants.javaファイルを変更して、新しい権限を追加します。

/**
 * Springの定数セキュリティ権限
 */
public final class AuthoritiesConstants {

    public static final String ADMIN = "ROLE_ADMIN";

    public static final String USER = "ROLE_USER";

    public static final String ANONYMOUS = "ROLE_ANONYMOUS";
    
    public static final String EXAMPLE_AUTHORITY = "ROLE_EXAMPLE_AUTHORITY";

    private AuthoritiesConstants() {
    }
}

新しいロールをauthority.csvに含めることを忘れないでください。

name
ROLE_ADMIN
ROLE_USER
ROLE_ANONYMOUS
ROLE_EXAMPLE_AUTHORITY

これで、SecurityConfiguration.javaで使用できるようになります。

@Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        http
            .csrf()
            .disable()
            ...
        ...
        .and()
            .authorizeRequests()
            ...
            .antMatchers("/newresource/**").hasAuthority(AuthoritiesConstants.ROLE_EXAMPLE_AUTHORITY)

コントローラレイヤ(例:FrontPageConfigResource.java)では、次のようになります。

@DeleteMapping("/order-items/{id}")
@Timed
@PreAuthorize("hasAuthority('ROLE_EXAMPLE_AUTHORITY')")
public ResponseEntity<Void> deleteOrderItem(@PathVariable Long id) {
    ...
}

Angular htmlファイルでは、次のようになります。jhiHasAnyAuthority=[‘ROLE_ADMIN’, ‘ROLE_EXAMPLE_AUTHORITY’ ...]

And in your Angular routes:

export const messageRoute: Routes = [
    {
        path: 'message',
        component: MessageComponent,
        data: {
            authorities: ['ROLE_EXAMPLE_AUTHORITY'],
            pageTitle: 'Messages'
        },
        canActivate: [UserRouteAccessService]
    }
];

オープンソースの例はJhipsterPress: https://github.com/Tonterias/JhipsterPress にあります。