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

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

Spring - 在調(diào)用控制器的方法之前執(zhí)行代碼

Spring - 在調(diào)用控制器的方法之前執(zhí)行代碼

米琪卡哇伊 2022-05-21 20:19:00
在調(diào)用控制器中的方法之前,是否有任何類似于@PreAuthorize或@PreFilter我可以用來(lái)運(yùn)行代碼的注釋?我需要將信息添加到請(qǐng)求上下文(特定于被調(diào)用的方法),然后由ExceptionHandler.例如@RestControllerpublic MyController{  @UnkwonwAnnotation("prepareContext(request.getAgentId())"){  public ResponseEntity method1(RequestA requestA) {    ...  }  @UnkwonwAnnotation("prepareContext(request.getUserName())"){  public ResponseEntity method1(RequestB requestB) {    ...  }}我實(shí)際上可以使用@PreAuthorize但感覺(jué)不對(duì)
查看完整描述

3 回答

?
拉丁的傳說(shuō)

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

您可以為此添加攔截器


樣本攔截器


public class CustomInterceptor implements HandlerInterceptor {


    

    @Override

    public boolean preHandle(HttpServletRequest request,HttpServletResponse  response) {

    //Add Login here 

        return true;

    }

配置


@Configuration

public class MyConfig extends WebMvcConfigurerAdapter {

    @Override

    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new MyCustomInterceptor()).addPathPatterns("/**");

    }

}

希望這可以幫助


查看完整回答
反對(duì) 回復(fù) 2022-05-21
?
哆啦的時(shí)光機(jī)

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

也許一個(gè)不錯(cuò)的選擇是實(shí)現(xiàn)一個(gè)自定義過(guò)濾器,該過(guò)濾器在每次收到請(qǐng)求時(shí)運(yùn)行。


您需要擴(kuò)展“OncePerRequestFilter”并覆蓋方法“doFilterInternal”


public class CustomFilter extends OncePerRequestFilter {


    @Override

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,

                                    FilterChain filterChain) throws ServletException, IOException {


        //Add attributes to request

        request.getSession().setAttribute("attrName", new String("myValue"));


        // Run the method requested by petition

        filterChain.doFilter(request, response);


        //Do something after method runs if you need.


    }

}

在您必須在 Spring 中使用 FilterRegistrationBean 注冊(cè)過(guò)濾器之后。如果你有 Spring 安全,你需要在安全過(guò)濾器之后添加你的過(guò)濾器。


查看完整回答
反對(duì) 回復(fù) 2022-05-21
?
Helenr

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

Spring Aspect 也是在控制器之前執(zhí)行代碼的好選擇。


@Component

@Aspect

public class TestAspect {


    @Before("execution(* com.test.myMethod(..)))")

    public void doSomethingBefore(JoinPoint jp) throws Exception {


        //code  

    }

}

這里myMethod()將在控制器之前執(zhí)行。


查看完整回答
反對(duì) 回復(fù) 2022-05-21
  • 3 回答
  • 0 關(guān)注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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