使用Spring Security时如何有选择地动态禁用oauth?

时间:2018-08-16 19:43:04

标签: java spring spring-security spring-security-oauth2 spring-oauth2

我在属性文件secureAPI中有一个变量,该变量已加载到Environment.isSecureAPI() => boolean中并可以通过true访问,并且我正在尝试使用该变量来启用/禁用Spring Security。 (false表示需要身份验证,spring-security.xml表示不受限制的访问)。我尝试重写几个类,并在根本没有提供令牌的情况下设法绕过身份验证,但是如果实际上传递了令牌,则绕过安全性会有麻烦。作为最后的选择,我什至尝试以编程方式生成一个新的有效令牌来代替使用,但也不能完全正常工作。

这是我开始弄乱事物之前的原始<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2" xmlns:sec="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <http pattern="/path/to/resource*" xmlns="http://www.springframework.org/schema/security" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint" use-expressions="true" access-decision-manager-ref="accessDecisionManager"> <anonymous enabled="false" /> <intercept-url pattern="/path/to/resource*" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" /> <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /> <access-denied-handler ref="oauthAccessDeniedHandler" /> </http> <!-- ... more of the same, with different paths/resources ... --> <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="realmName" value="test" /> </bean> <bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="realmName" value="test/client" /> <property name="typeName" value="Basic" /> </bean> <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /> <bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"> <property name="authenticationManager" ref="clientAuthenticationManager" /> </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.RoleVoter" /> <bean class="org.springframework.security.access.vote.AuthenticatedVoter" /> <bean class="org.springframework.security.web.access.expression.WebExpressionVoter"/> </list> </constructor-arg> </bean> <authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security"> <authentication-provider user-service-ref="clientDetailsUserService" /> </authentication-manager> <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security"> <authentication-provider> <jdbc-user-service data-source-ref="jdbcTemplate" users-by-username-query= "select username, password, enabled from users where username=?" authorities-by-username-query= "select username, role from user_roles where username=? " /> </authentication-provider> </authentication-manager> <bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService"> <constructor-arg ref="clientDetails" /> </bean> <bean id="clientDetails" class="org.springframework.security.oauth2.provider.JdbcClientDetailsService"> <constructor-arg ref="jdbcTemplate" /> </bean> <bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.JdbcTokenStore"> <constructor-arg ref="jdbcTemplate" /> </bean> <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"> <property name="tokenStore" ref="tokenStore" /> <property name="supportRefreshToken" value="true" /> <property name="clientDetailsService" ref="clientDetails" /> </bean> <bean id="jdbcTemplate" class="com.myProject.SpringSecurityDataSource" /> <!-- this is already working, btw --> <bean id="userApprovalHandler" class="org.springframework.security.oauth2.provider.approval.TokenServicesUserApprovalHandler"> <property name="tokenServices" ref="tokenServices" /> </bean> <oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices" user-approval-handler-ref="userApprovalHandler"> <oauth:authorization-code /> <oauth:implicit /> <oauth:refresh-token /> <oauth:client-credentials /> <oauth:password /> </oauth:authorization-server> <oauth:resource-server id="resourceServerFilter" resource-id="unite" token-services-ref="tokenServices" /> <sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true"> <sec:expression-handler ref="oauthExpressionHandler" /> </sec:global-method-security> <oauth:expression-handler id="oauthExpressionHandler" /> <oauth:web-expression-handler id="oauthWebExpressionHandler" /> </beans> 文件:

Kotlin

在这里完成我想做的最好的方法是什么?

0 个答案:

没有答案