![](/img/trans.png)
[英]Spring MVC. Define custom HttpMessageConverter over XML configuration file
[英]How to define a custom AuthenticationEntryPoint without XML configuration
我已經掙扎了幾天了。 我是 Spring Boot 的新手,喜歡不使用 XML 配置的想法。
我創建了一個 RESTfull 應用程序(使用 JSON)。 我正在按照本教程正確配置身份驗證。
我想我設法使用 Java 配置復制了幾乎所有的配置,除了一件事 - AuthenticationEntryPoint
本教程使用這樣的http
標簽中的屬性,並通過以下方式定義formLogin :
<http entry-point-ref="restAuthenticationEntryPoint">
<intercept-url pattern="/api/admin/**" access="ROLE_ADMIN"/>
<form-login
authentication-success-handler-ref="mySuccessHandler"
authentication-failure-handler-ref="myFailureHandler"
/>
<logout />
</http>
Spring Security 手冊中的AuthenticationEntryPoint解釋說:
AuthenticationEntryPoint 可以使用 < http > 元素上的 entry-point-ref 屬性進行設置。
沒有提到有關如何使用 Java 配置進行操作的任何信息。
那么如何在沒有 XML 的情況下“注冊”我自己的restAuthenticationEntryPoint
以防止在使用formLogin時重定向到登錄表單?
下面我將提到我嘗試過的內容。
謝謝你們。
在我的嘗試中,發現您可以使用basicAuth定義它,如下所示:
@Configuration
@Order(1)
public static class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
if (restAuthenticationEntryPoint == null) {
restAuthenticationEntryPoint = new RestAuthenticationEntryPoint();
}
http
.authorizeRequests()
.antMatchers("/**").hasAnyRole(Sec.ADMIN,Sec.SUPER_USER)
...
.and()
.httpBasic()
.authenticationEntryPoint(restAuthenticationEntryPoint)
但我正在使用這樣的表單登錄(沒有httpBasic部分):
.and()
.formLogin()
.successHandler(mySavedRequestAwareAuthenticationSuccessHandler)
.failureHandler(simpleUrlAuthenticationFailureHandler)
問題是它在沒有收到憑據時重定向到登錄表單。 由於這是一項 REST 服務,因此不應該。
FormLoginConfigurer
(類.formLogin()
使用)的文檔說:
創建的共享對象
填充了以下共享對象
AuthenticationEntryPoint
但找不到覆蓋它的方法。
有任何想法嗎?
聚苯乙烯
不要認為將登錄表單覆蓋為僅返回錯誤的自定義表單是一個好主意。
您提供的參考文檔中的引用將您指向http.exceptionHandling()
。 您可以在那里設置共享入口點。
http.exceptionHandling().authenticationEntryPoint(myEntryPoint);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.