Spring security Spring安全OAuth2事件日志
我们要求在应用程序中包含OAuth2。为此,我们选择了spring安全性作为一种方法。我已经从SpringSecurityProjects页面查看了Spraklr2和Tonr2示例项目。它工作得很好 我们的新要求是,作为outh2提供者,我们必须保存每个请求日志(来自资源所有者的请求)。。我们必须保存客户端id、资源所有者用户名、请求的url(资源服务器上的资源)、授权等 我在谷歌上搜索了一段时间,但没有找到任何线索 有人能帮我想出什么办法来达到这个目的吗Spring security Spring安全OAuth2事件日志,spring-security,oauth-2.0,Spring Security,Oauth 2.0,我们要求在应用程序中包含OAuth2。为此,我们选择了spring安全性作为一种方法。我已经从SpringSecurityProjects页面查看了Spraklr2和Tonr2示例项目。它工作得很好 我们的新要求是,作为outh2提供者,我们必须保存每个请求日志(来自资源所有者的请求)。。我们必须保存客户端id、资源所有者用户名、请求的url(资源服务器上的资源)、授权等 我在谷歌上搜索了一段时间,但没有找到任何线索 有人能帮我想出什么办法来达到这个目的吗 提前感谢如果您的意思是希望将所有请求记
提前感谢如果您的意思是希望将所有请求记录到/oauth/authorize和/oauth/token,那么可以通过实现自己的端点来实现,该端点将分别将调用委托给AuthorizationEndpoint和TokenEndpoint
您必须在XML文件中配置它,当然…如果您没有找到答案,下面是我用来拦截请求的方法
<http path-type="regex"
create-session="never" entry-point-ref="oauthAuthenticationEntryPoint"
access-decision-manager-ref="accessDecisionManager"
use-expressions="true"
xmlns="http://www.springframework.org/schema/security">
<anonymous enabled="false" />
<custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="myFilterSecurityInterceptor" />
<intercept-url pattern="/soap/*" access="isAnonymous()" method="GET" />
<intercept-url pattern="/advisor/[0-9a-zA-Z_]/all/clients/[0-9]/[0-9]" access="isFullyAuthenticated()"/>
<custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
<expression-handler ref="oauthWebExpressionHandler" />
</http>
<bean id="myFilterSecurityInterceptor" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="accessDecisionManager" ref="affirmativeBasedAccessDecisionManager"/>
<property name="securityMetadataSource" ref="myCustomBean"/>
</bean>
<bean id="affirmativeBasedAccessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
<constructor-arg>
<list>
<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"/>
<bean id="authenticatedVoter" class="org.springframework.security.access.vote.AuthenticatedVoter"/>
</list>
</constructor-arg>
</bean>
<bean id="myCustomBean" class="MyCustomClass">
<constructor-arg>
<util:map />
</constructor-arg>
</bean>
<authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"select username,password, enabled from oauth_users where username=?"
authorities-by-username-query=
"select username, role from oauth_user_roles where username =? " />
</authentication-provider>
</authentication-manager>
<bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
<property name="realmName" value="realm" />
</bean>
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans">
<constructor-arg>
<list>
<bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" />
<bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
<bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/>
</list>
</constructor-arg>
</bean>
<bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />
<oauth:web-expression-handler id="oauthWebExpressionHandler" />
公共类MyCustomClass扩展了DefaultFilterInocationSecurityMetaDataSource{
public APILibSecurityMetadataSource(
LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap) {
super(requestMap);
}
@Override
public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
System.out.println(" My code in MyCustomClass Interceptor");
}
公共APILibSecurityMetadataSource(
LinkedHashMap请求映射){
超级地图;
}
@凌驾
公共集合getAttributes(对象对象)引发IllegalArgumentException{
System.out.println(“MyCustomClass拦截器中的我的代码”);
}
}当用户使用Spring Oauth进行身份验证时,它会将事件发布到
AuthenticationEventPublisher
。您可以创建一个实现AuthenticationEventPublisher
的组件,如下所示:
@Component
public class AuditEvent implements AuthenticationEventPublisher{
@Override
public void publishAuthenticationSuccess(Authentication authentication) {
if(authentication instanceof UsernamePasswordAuthenticationToken) {
log(authentication.getName(), "Authentication successful");
}
}
@Override
public void publishAuthenticationFailure(AuthenticationException exception, Authentication authentication) {
log(authentication.getName(),"Authentication Failure:");
}
}
并检查身份验证是否属于类型
UsernamePasswordAuthenticationToken
。这将为您提供登录事件,您可以根据需要登录。我想在数据库中为每个资源所有者请求保存一条记录。我需要实现一个类(可能是拦截器)来完成这项工作。类/拦截器应该拦截每个请求,并捕获客户端id、请求的用户和请求的url。我们只是想维护我们服务的日志。因此,我们可以生成一个报告,其中包含每个用户的请求数、每个客户端的请求数以及最常用的资源。。等不确定你的回答是否与我的问题相关..你想拦截的请求是什么?通常,oauth服务器只会发出访问令牌。它唯一的API是/authorize和/token。同意?需要拦截对基于spring REST的应用程序的传入请求,该应用程序使用spring security OAuth2进行身份验证/授权。我想知道哪个用户点击哪个url,通过哪个客户端。在我基于REST的应用程序和我的应用程序的服务器客户端中有几个URL。我所有的客户都有授权码授权类型。通过实现我自己的端点,我只能截获与身份验证/授权相关的请求,对吗?。但我想拦截对我的REST应用程序的所有请求。如果我不清楚,我还是很抱歉。