[英]Spring Security shows default login instead of OAuth2
我正在编写需要验证用户身份的RESTful
Web服务(在Tomcat上运行的Jersey)。 计划是将它们重定向为通过OAuth2
登录到Google。 一旦他们授予我们查看其电子邮件地址的权限,我们就会知道他们是否是系统中的已知用户。
我正在使用Spring Security
。 我使用了基本身份验证(用户和密码的硬连接列表)。然后,我在XML配置中添加了OAuth2
元素,但是当我从浏览器访问该服务时,仍然会提示浏览器登录重定向到Google的网站。 控制台没有记录任何特定的错误。
建议将不胜感激。 这是我的Spring Security配置文件:spring-security.xml(尽管没有真实客户端的ID和密码)。
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/security/oauth2
http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd">
<debug/>
<oauth:client id="oauth2ClientFilter" />
<oauth:resource id="googleOauth2Resource" type="authorization_code" client-id="myclientid.apps.googleusercontent.com" client-secret="myclientsecret"
access-token-uri="https://accounts.google.com/o/oauth2/v3/token" user-authorization-uri="https://accounts.google.com/o/oauth2/auth" scope="email" />
<http auto-config='true' xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/V1/**" access="ROLE_USER" />
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />
</http>
<oauth:rest-template id="googleOauthRestTemplate" resource="googleOauth2Resource" />
<authentication-manager>
</authentication-manager>
</beans:beans>
这是我的web.xml
文件的胆量。 (与我列出的用户名和密码的硬列表相比,这里没有任何改变。)
<!-- Require HTTPS for everything except /img (favicon) and /css. -->
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOnly</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPSOrHTTP</web-resource-name>
<url-pattern>*.ico</url-pattern>
<url-pattern>/img/*</url-pattern>
<url-pattern>/css/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.foobar.dataservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/V1/*</url-pattern>
</servlet-mapping>
<resource-ref>
<description>TAE DB Connection Pool</description>
<res-ref-name>jdbc/taeDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-security.xml</param-value>
</context-param>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
对于OAuth2
正常运行的建议,我们将不胜感激。
从spring-security.xml中的http元素中删除auto-config =“ true”摆脱了错误的登录表单。
这揭示了一个新错误:
配置问题:无法建立AuthenticationEntryPoint。 请确保您具有通过名称空间配置的登录机制(例如form-login),或使用'entry-point-ref'属性指定自定义AuthenticationEntryPoint
所以我添加了缺少的entry-point-ref
和access-denied-handler
:
<http xmlns="http://www.springframework.org/schema/security" entry-point-ref="oauthAuthenticationEntryPoint">
<intercept-url pattern="/V1/**" access="IS_AUTHENTICATED_FULLY" />
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<beans:bean id="oauthAuthenticationEntryPoint"
class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
</beans:bean>
<beans:bean id="oauthAccessDeniedHandler"
class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler">
</beans:bean>
现在,该服务将启动,而不会记录任何错误,并且不会显示错误的登录表单。
(但是请注意,它拒绝了我所有资源的许可。我认为Spring Security
会代表我的RESTful
服务重定向到Google,但是也许我的网页需要在调用服务之前获得授权令牌?我认为这是一个新话题。 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.