第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何發(fā)送未經(jīng)授權的響應以進行注釋@CurrentUser

如何發(fā)送未經(jīng)授權的響應以進行注釋@CurrentUser

慕尼黑8549860 2022-08-17 10:18:29
如何發(fā)送未經(jīng)授權的注釋響應@CurrentUser我有注釋@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)public @interface CurrentUser {    boolean required() default true;}具有參數(shù)解析器public class CurrentUserIdMethodArgumentResolver extends AbstractCurrentUserMethodArgumentResolver<CurrentUserId> {    public CurrentUserIdMethodArgumentResolver() {        super(CurrentUserId.class, null);    }    @Override    protected boolean isRequired(CurrentUserId annotation) {        return annotation.required();    }    @Override    protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {        return (getCurrentUser() != null)? getCurrentUser().getId() : null;    }}配置彈簧安全性  @Override    protected void configure(HttpSecurity http) throws Exception {        http                .authorizeRequests()                            .antMatchers(REACT_API_PERMITTED_URL, PERMITTED_SOCKET_PUBLIC_TOPIC, PERMITTED_SOCKET_ENDPOINT1, PERMITTED_SOCKET_ENDPOINT2).permitAll()                            .antMatchers(SOCKET_PRIVATE_ENDPOINT, NOT_PERMITTED_SOCKET_ENDPOINT1, NOT_PERMITTED_SOCKET_ENDPOINT2).authenticated()                            .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")                            .antMatchers("/moderator/**").access("hasRole('ROLE_MODERATOR')")                            .anyRequest().authenticated()                .and().headers()                        .frameOptions().sameOrigin()                .and().formLogin()    }我希望在我的控制器中返回到HTTP。STATUS.未經(jīng)授權調(diào)用它(如果用戶未獲得授權) @GetMapping("/test") public User test(@CurrentUser User current) {return current}現(xiàn)在我有狀態(tài)400,錯誤的請求,但想要配置這個狀態(tài)
查看完整描述

2 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

Spring已經(jīng)有了這個,只需添加到您的配置中,并使用特殊注釋等注釋您的安全方法:@EnableGlobalMethodSecurity(prePostEnabled = true)@PreAuthorize("isAuthenticated()")@PreAuthorize("hasAnyRole('ADMIN)")


@EnableGlobalMethodSecurity(prePostEnabled = true)

@Configuration

public class WebSecurityConf43547 extends WebSecurityConfigurerAdapter {

    @Override

    protected void configure(HttpSecurity http) throws Exception {

    ....

    }

}

和控制器中


@GetMapping("/test")

@PreAuthorize("isAuthenticated()") //this annotation better add to service method @Service

public String test() {

    return "abc"

}

或 import org.springframework.security.core.Authentication;


@GetMapping("/test")

public String getOk(Authentication authentication) {

   return authentication.getName();

}


查看完整回答
反對 回復 2022-08-17
?
紅糖糍粑

TA貢獻1815條經(jīng)驗 獲得超6個贊

我決定它的問題,所以:


@Configuration

@EnableWebMvc

public class WebConfig extends WebMvcConfigurerAdapter {


    @Bean

    public CurrentUserMethodArgumentResolver userMethodArgumentResolver() {

        return new CurrentUserMethodArgumentResolver() {

            @Override

            protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {

                SecurityContext securityContext = SecurityContextHolder.getContext();

                CurrentUser annotation = parameter.getParameterAnnotation(CurrentUser.class);

                boolean anonymousUser = securityContext.getAuthentication() instanceof AnonymousAuthenticationToken;

                if (annotation.required() && anonymousUser) {

                    throw new BadCredentialsException("access is denied");

                }

                return super.resolveName(name, parameter, request);

            }

        };

    }


    @Override

    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> list) {

        list.add(userMethodArgumentResolver());

        super.addArgumentResolvers(list);

}


查看完整回答
反對 回復 2022-08-17
  • 2 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號