Spring Security @PreAuthorization pass enums in directly

前端 未结 4 1499
清歌不尽
清歌不尽 2021-02-07 03:29

My question is a duplicate of Custom annotation with spring security but it went unanswered and I believe there should be a simple solution to the problem.

Basically ins

4条回答
  •  -上瘾入骨i
    2021-02-07 04:07

    I did that way :

    1 - Define your enum referencing a public final static String "VALUE" like this

    public enum MyEnum {
        ENUM_A(Names.ENUM_A);
    
        private String value;
    
        private MyEnum (String value) {
            this.value = value;
        }
    
        public static class Names {
    
            public  final static String ENUM_A = "ENUM_A";
        }
    }
    

    2 - Concat MyEnum values in @PreAuthorize

    @PreAuthorize("hasPermission('myDomain', '"+ MyEnum.Names.ENUM_A+"')")
    

提交回复
热议问题