有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java HTML转义字符串

Spring有htmlEscape可以从用户输入中避免潜在的不安全HTML。我想将htmlEscape应用于后端基于Spring的代码(similar concept)的注释。这有助于防止XSS攻击

class MyCreateRequestDoc {
    @NotBlank(message = 'A name must be specified')
    @AsSafeHtml
    String name

我相信这需要一个AOP模式,但我不知道如何在JavaSpring中实现这样的东西

我从以下几点开始:

package com.my.company.services..security

import groovy.util.logging.Slf4j
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.stereotype.Component
import org.springframework.web.util.HtmlUtils

@Aspect
@Slf4j
@Component
class AsSafeHtmlAspect {
  @Around("@within(com.my.company.services.security.AsSafeHtml)")
  void assertProperty(ProceedingJoinPoint pjp){
      var input = 'get this from somewhere'
      return toSafeHtml(input) //return?
  }

  private String toSafeHtml(input) {
      return HtmlUtils.htmlEscape(input)
  }
}

以及上面Around中使用的相关注释:

package com.my.company.services.security

import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy

@Retention(RetentionPolicy.RUNTIME)
@interface AsSafeHtml {

}

共 (0) 个答案