2016-04-09 18 views
0

Bir SpringBoot uygulamasında oturumda bazı varsayılan değerleri ayarlamak istiyorum. İdeal olarak, varsayılan değerleri ayarlamak için @ControllerAdvice ile açıklanmış bir not kullanmayı düşünüyordum. Bu, özellikle kod snippet'inin tüm sayfalar için çalıştırılması gerektiğinden kullanışlıdır.Bir SpringBoot uygulamasında bir ControllerAdvice'de HttpSession'a erişme

HttpSession ürününe @ControllerAdvice ek açıklamalı bir sınıfa erişmenin bir yolu var mı?

+0

Neden bunun için Interceptors kullanmayın? – JSONStatham

+0

@JSONStatham bu mükemmel bir fikir! –

cevap

1

Sen kullanarak, @ControllerAdvice içinden oturumu alabilirsiniz:

Seçenek 1:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); 

HttpSession session = requeset.getSession(true);//true will create if necessary 

Seçenek 2:

@Autowired(required=true) 
private HttpServletRequest request; 

Seçenek 3:

@Context 
private HttpServletRequest request; 

İşte bir sınav var Tüm denetleyici son nokta yöntemlerini kesiştiren bir Denetleyici yönünü nasıl devrettiğimden emin olun:

@Component 
@Aspect 
class ControllerAdvice{ 

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") 
    void hasRequestMappingAnnotation() {} 

    @Pointcut("execution(* your.base.package..*Controller.*(..))") 
    void isMethodExecution() {} 

    /** 
    * Advice to be executed if this is a method being executed in a Controller class within our package structure 
    * that has the @RequestMapping annotation. 
    * @param joinPoint 
    * @throws Throwable 
    */ 
    @Before("hasRequestMappingAnnotation() && isMethodExecution()") 
    void beforeRequestMappedMethodExecution(JoinPoint joinPoint) { 
     String method = joinPoint.getSignature().toShortString(); 
     System.out.println("Intercepted: " + method); 

     //Now do whatever you need to 
    } 
} 
+0

Bu gerçekten çok hoş. Neyse, getRequest() 'mevcut değil. Üstelik, [this] 'dan (http://stackoverflow.com/a/1629239/1983997) “getRequestAttributes()' yerine ”currentRequestAttributes()” kullanılmasının gerektiğini görüyorum.) –

+0

Ayrıca, yönteme nasıl açıklama yapmalıyım? her bir kontrolör çağrıldığında kontrol edilebilmesi için controlleradvice'de? –

+0

getRequestAttributes ve getCurrentRequestAttrributes, getCurrentRequestAttributes: Varsa, önceden bağlı RequestAttributes örneğini gösterir. Varsa mevcut JSF FacesContext'e geri döner. – pczeus

0

Daha sonra @ControllerAdvice yerine Spring Interceptors kullanmanızı öneririm. Daha sonra, Interceptor eşleştirmeleri ile kolayca davranışı özelleştirebilirsiniz. Eğer küresel bazı istisnalar işlemek istediğinizde

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-handlermapping-interceptor

@ControllerAdvice gerçekten parlar.

+0

Ayrıca, önleme sınıfının oturumu nasıl kestiğini gösterebilir misiniz? –

+0

Oturum yönetimi için tam olarak ne kullanıyorsunuz? Redis ile bahar seansı mı? – JSONStatham

+0

Evet, yerleşik bir REDIS örneğim var. Ama bunun bir şeyi değiştirdiğini düşünmüyorum, değil mi? –

İlgili konular