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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

慕尼黑8549860 2022-08-17 10:18:29
如何發(fā)送未經(jīng)授權(quán)的注釋響應(yī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)授權(quán)調(diào)用它(如果用戶未獲得授權(quán)) @GetMapping("/test") public User test(@CurrentUser User current) {return current}現(xiàn)在我有狀態(tài)400,錯(cuò)誤的請(qǐng)求,但想要配置這個(gè)狀態(tài)
查看完整描述

2 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

Spring已經(jīng)有了這個(gè),只需添加到您的配置中,并使用特殊注釋等注釋您的安全方法:@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();

}


查看完整回答
反對(duì) 回復(fù) 2022-08-17
?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

我決定它的問題,所以:


@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);

}


查看完整回答
反對(duì) 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 142 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)