javascript
spring启动执行_执行器的Spring启动和安全性事件
spring啟動執行
Spring Boot Actuator提供了審核功能,用于在啟用了Spring Security的Spring Boot應用程序中發布和偵聽與安全相關的事件。 默認事件是身份驗證成功,身份驗證失敗和訪問被拒絕,但是可以使用自定義事件進行擴展。
確保在項目中啟用了Spring Boot Security和Actuator
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency>執行器
默認情況下, /auditevents端點/auditevents啟用狀態,因此在啟動應用程序(并使用應用程序日志中提供的用戶名user和password登錄)后,您可以看到當前的安全事件:
{"events": [{"timestamp": "2017-03-14T22:59:58+0000","principal": "user","type": "AUTHENTICATION_FAILURE","data": {"details": {"remoteAddress": "0:0:0:0:0:0:0:1","sessionId": null},"type": "org.springframework.security.authentication.BadCredentialsException","message": "Bad credentials"}},{"timestamp": "2017-03-14T23:00:07+0000","principal": "user","type": "AUTHENTICATION_SUCCESS","data": {"details": {"remoteAddress": "0:0:0:0:0:0:0:1","sessionId": null}}}] }/auditevents端點接受請求的可選參數:
- pricipal -主體名稱
- after –事件發生后的日期,格式如下: yyyy-MM-dd'T'HH:mm:ssZ
- type –事件類型(例如AUTHORIZATION_FAILURE,AUTHENTICATION_SUCCESS,AUTHENTICATION_FAILURE,AUTHENTICATION_SWITCH)
請求示例:
http:// localhost:8080 / auditevents?type = AUTHORIZATION_FAILURE&after = 2017-03-14T23%3A14%3A12%2B0000&principal = anonymousUser
端點實現使用org.springframework.boot.actuate.audit.AuditEventRepository返回所有已注冊的審核事件。
- 自定義/auditevents端點
您可以使用endpoints.auditevents.*屬性來自定義端點。 例如,要更改審核事件端點的路徑,只需使用endpoints.auditevents.path屬性。
使用
安全事件由執行器中的org.springframework.boot.actuate.audit.AuditEvent值對象表示。 該對象包含時間戳,用戶名,事件類型和事件數據。
獲得有關審核事件的最簡單方法是通過Spring的org.springframework.context.event.EventListener訂閱org.springframework.boot.actuate.audit.listener.AuditApplicationEvent事件:
@Component public class AuditApplicationEventListener {private static final Logger LOG = LoggerFactory.getLogger(AuditApplicationEventListener.class);@EventListenerpublic void onAuditEvent(AuditApplicationEvent event) {AuditEvent actualAuditEvent = event.getAuditEvent();LOG.info("On audit application event: timestamp: {}, principal: {}, type: {}, data: {}",actualAuditEvent.getTimestamp(),actualAuditEvent.getPrincipal(),actualAuditEvent.getType(),actualAuditEvent.getData());} }輸出示例:
2017-03-15 00:44:12.921 INFO 13316 --- [nio-8080-exec-1] p.c.d.s.s.AuditApplicationEventListener : On audit event: timestamp: Wed Mar 15 00:44:12 CET 2017, principal: user, type: AUTHENTICATION_SUCCESS, data: {details=org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null}異步事件
@EventListener是同步的,但是如果需要異步行為,則可以使用@Async注釋事件偵聽器方法,并確保啟用了異步(例如,通過@EnableAsync ):
@Component public class AuditApplicationEventListener {private static final Logger LOG = LoggerFactory.getLogger(AuditApplicationEventListener.class);@EventListener@Asyncpublic void onAuditEvent(AuditApplicationEvent event) {} }并配置:
@SpringBootApplication @EnableAsync public class Application {public static void main(String[] args) {SpringApplication.run(Application.class);} }使用
另外,您可以擴展org.springframework.boot.actuate.audit.listener.AbstractAuditListener并覆蓋其org.springframework.boot.actuate.audit.listener.AbstractAuditListener#onAuditEvent方法:
@Component public class AuditEventListener extends AbstractAuditListener {private static final Logger LOG = LoggerFactory.getLogger(AuditEventListener.class);@Overrideprotected void onAuditEvent(AuditEvent event) {LOG.info("On audit event: timestamp: {}, principal: {}, type: {}, data: {}",event.getTimestamp(),event.getPrincipal(),event.getType(),event.getData());} }注意:事件存儲庫中不會存儲任何事件,因此/auditevents端點將始終返回空數組。 要解決此問題,您可以注入審核存儲庫,也可以直接從org.springframework.boot.actuate.audit.listener.AuditListener擴展:
@Component public class AuditEventListener extends AbstractAuditListener {private static final Logger LOG = LoggerFactory.getLogger(AuditEventListener.class);@Autowiredprivate AuditEventRepository auditEventRepository;@Overrideprotected void onAuditEvent(AuditEvent event) {LOG.info("On audit event: timestamp: {}, principal: {}, type: {}, data: {}",event.getTimestamp(),event.getPrincipal(),event.getType(),event.getData());auditEventRepository.add(event);} }與事件發布者發布自己的審核事件
在下面的示例中,使用了應用程序事件發布者( org.springframework.context.ApplicationEventPublisher )來發布類型為CUSTOM_AUDIT_EVENT的自定義審核事件。 新的偵聽器方法僅偵聽那些新事件,而先前的方法將忽略它們(請注意,這只是一個示例)。 與其他事件一樣,自定義事件將使用審核事件存儲庫進行存儲。
@Component public class AuditApplicationEventListener {private static final Logger LOG = LoggerFactory.getLogger(AuditApplicationEventListener.class);@Autowiredprivate ApplicationEventPublisher applicationEventPublisher;@EventListener(condition = "#event.auditEvent.type != 'CUSTOM_AUDIT_EVENT'")@Asyncpublic void onAuditEvent(AuditApplicationEvent event) {AuditEvent actualAuditEvent = event.getAuditEvent();LOG.info("On audit application event: timestamp: {}, principal: {}, type: {}, data: {}",actualAuditEvent.getTimestamp(),actualAuditEvent.getPrincipal(),actualAuditEvent.getType(),actualAuditEvent.getData());applicationEventPublisher.publishEvent(new AuditApplicationEvent(new AuditEvent(actualAuditEvent.getPrincipal(), "CUSTOM_AUDIT_EVENT")));}@EventListener(condition = "#event.auditEvent.type == 'CUSTOM_AUDIT_EVENT'")public void onCustomAuditEvent(AuditApplicationEvent event) {LOG.info("Handling custom audit event ...");} }注意示例代碼
可以在spring-boot-thymeleaf存儲庫中找到本文的示例代碼。 默認情況下,兩個配置文件均禁用安全性。 通過更改application.properties的security.basic.enabled屬性來啟用它。
翻譯自: https://www.javacodegeeks.com/2017/03/spring-boot-security-events-actuator.html
spring啟動執行
總結
以上是生活随笔為你收集整理的spring启动执行_执行器的Spring启动和安全性事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QQ情侣空间怎么设置?教你玩转QQ情侣空
- 下一篇: slice_Spring Boot We