2016-07-12 28 views
0

我正在開發一個使用spring的WebSocket服務器應用程序。 類PlayerHandlerSpring aop方面沒有在註釋上執行

import org.springframework.web.socket.WebSocketSession; 
import org.springframework.web.socket.handler.TextWebSocketHandler; 

import java.io.IOException; 

/** 
* Created by kris on 11.07.16. 
*/ 
public class PlayerHandler extends TextWebSocketHandler{ 
    public PlayerHandler(){} 

    @Override 
    @AuthorizationRequired 
    public void handleTextMessage(WebSocketSession session, TextMessage tm) throws IOException { 
     session.sendMessage(tm); 
    } 
} 

我想用戶與由令牌每個傳入請求被授權,所以我創建一個方面UserAuthorization

package com.berrigan.axevor.authorization; 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.springframework.stereotype.Component; 


@Aspect 
@Component 
public class UserAuthorization { 
    @Around("@annotation(com.berrigan.axevor.authorization.AuthorizationRequired)") 
    public void authorize(ProceedingJoinPoint jp) throws Throwable{ 
     System.out.println("\n\n\n\n\Works\n\n\n\n\n\n"); 
     jp.proceed(); 
    } 
} 

我添加了註釋@AuthorizationRequired,其指示在哪些用戶的方法將被授權。不幸的是方法授權永遠不會被調用。我在主類中添加了以下代碼來檢查bean是否被創建。

UserAuthorization ua = ctx.getBean(UserAuthorization.class); // ApplicationContext 
if(au == null) System.out.println("is null") 

但我沒有得到這樣的日誌。 我的Spring配置

@EnableAutoConfiguration 
@Configuration 
@EnableAspectJAutoProxy 
@Import({com.berrigan.axevor.websocket.WebSocketConfig.class}) 
@ComponentScan(basePackages = {"com.berrigan.axevor"}) 
public class Config {} 

註釋代碼:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface AuthorizationRequired{} 

@Configuration @EnableWebSocket

public class WebSocketConfig implements WebSocketConfigurer{ 
    @Override 
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry){ 
     registry.addHandler(playerHandler(), "/game").setAllowedOrigins("*"); 
    } 

    @Bean 
    public WebSocketHandler playerHandler(){ 
     return new PlayerHandler(); 
    } 
} 
+1

讓我們看看你的註釋。發佈[mcve],而你在它。 –

+0

是的,這一切對我來說都很好,有一些基本的假設。請發佈[mcve]。 –

+0

這段代碼應該是可運行的,我使用的是spring-boot來運行它 – Berrigan

回答

0

發現的解決方案,損壞pom.xml文件。再生後,一切都像魅力