2
CXF 기반의 나머지 API를 보호하려고합니다. 내 구성이 기술적으로 작동하는 동안 401에서 HTML 메시지가 아닌 JSON으로 응답하는 API를 얻을 수없는 것 같습니다. 몇 가지 다른 게시물을 기반으로 봄 보안 구성을 위해 groovy를 사용하여 다음 Java 구성을 함께 작성했습니다.Java 보안 설정을 사용하여 Rest API에 대한 스프링 보안 HTTP 기본 인증
@Configuration
@EnableWebSecurity
@Slf4j
class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) {
http.antMatcher('/api/**')
.authorizeRequests()
.antMatchers('/api/admin/**').hasRole('ADMIN')
.antMatchers('/api/**').hasRole('USER')
.and()
.httpBasic()
.and()
.addFilterBefore(
new BasicAuthenticationFilter(authenticationManager: authenticationManager(), authenticationEntryPoint: new BasicJsonEntryPoint(realmName: 'Local')),
BasicAuthenticationFilter.class
)
}
static class BasicJsonEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest req, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
log.debug 'Handling response'
response.addHeader HttpHeaders.WWW_AUTHENTICATE, /Basic realm="${getRealmName()}"/
response.setStatus HttpStatus.UNAUTHORIZED.value()
response.getWriter().println([status: HttpStatus.UNAUTHORIZED.value(), message: e.getMessage()].toJson())
}
}
}
나는이 일반적인 접근 방식에 대해 여러 가지 변형을 시도했지만, API에서 HTML을 다시 얻었습니다. 다음과 같은 필수/RESP 참조 :
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 401 Full authentication is required to access this resource</title>
</head>
<body>
<h2>HTTP ERROR 401</h2>
<p>Problem accessing /api/test. Reason:
<pre> Full authentication is required to access this resource</pre>
</p>
<hr />
<i>
<small>Powered by Jetty://</small>
</i>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
</body>
</html>
이 문제는 해결되지 않습니다. 나는 방파제, 톰캣, 언더 토우 (Undertow)와 함께 노력했다. 같은 결과. – Todd