Java 使用Spring安全性记录请求
如何通过Spring Security记录对Spring REST服务的请求?Java 使用Spring安全性记录请求,java,spring,spring-security,Java,Spring,Spring Security,如何通过Spring Security记录对Spring REST服务的请求? (给我举一些例子)一旦Spring Security正确设置并运行,您可以通过获取SecurityContext访问当前登录用户的凭据(或至少用户名),并从中获取用户详细信息: final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String username = null;
(给我举一些例子)一旦Spring Security正确设置并运行,您可以通过获取SecurityContext访问当前登录用户的凭据(或至少用户名),并从中获取用户详细信息:
final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = null;
if (principal instanceof UserDetails) {
username = ((UserDetails) principal).getUsername();
} else {
username = principal.toString();
}
log.debug("User: " + username + " has accessed the service");
我们在一个应用程序中做到了这一点。这是一个SpringMVC程序,但这并不重要
但是,我不会真正将用户名登录到某个文件中。您可以尝试设置某种协议数据库,并记录更多信息(“X在YY时间访问MYSERVICE做Z”之类的事情)web.xml
<filter>
<filter-name>securityFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityFilter</filter-name>
<url-pattern>/*</url-pattern>
<url-pattern>//</url-pattern>
</filter-mapping>
安全过滤器
org.springframework.web.filter.DelegatingFilterProxy
targetFilterLifecycle
真的
安全过滤器
/*
//
spring-config.xml
...
<bean class="com.examlple.MyLogFilter" id="logFilter"/>
...
<bean class="org.springframework.security.web.FilterChainProxy" id="securityFilter">
<sec:filter-chain-map path-type="ant">
<sec:filter-chain filters="logFilter,otherfilter1,otherfilter2" pattern="/myRESTservicePath/**"/>
....
。。。
...
....
MyLogFilter
是一个简单的servlet筛选器实现Amibiguous..您想记录服务被访问还是谁访问了您的服务?我想记录谁访问了服务