Spring Boot를 사용하여 RESTful 웹 서비스를 개발했습니다. URL을 입력하면 JSON 리소스가 반환됩니다. 서버 측은 완벽하게 JSON API를 준수하지는 않지만 작동합니다.
이제 간단한 HTTP 기본 인증으로 RESTful 서비스를 보호하고 싶습니다. 간단히 말해서 클라이언트 GET
가 액세스하기 위해 HTTP 를 보내는 경우
http://mycompany.com/restful/xyz
요청이 적절한으로 구성되어 있지 않으면 HTTP 인증되지 않은 오류를 수신합니다 Authorization basic XXXXXXXX
. xxxxxx는 user : pwd 의 암호화 된 형식입니다 .
저는 Spring Security로하고 싶었습니다. 인터넷 검색 후 다음과 같은 클래스를 만들어야 할 수도 있습니다.
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
....
}
@Override
protected void configure(HttpSecurity http) throws Exception {
....
}
}
이해해야 할 두 가지 사항이 있습니다.
내가 찾은 대부분의 Spring Security 샘플은 Spring MVC를 사용하는 웹 애플리케이션과 어떻게 든 관련이 있었지만 위에 표시된 시나리오에서 사용할 수 있다고 확신합니다-독립형 웹 서비스 (Tomcat에서는 괜찮지 만 웹 앱에서는 아님);
누구든지 특정 사용자 / 비밀번호 만 리소스에 필터를 전달할 수 있도록 허용되는 효과로 작동하는 위의 두 가지 방법에서 일부 코드 스 니펫을 보여줄 수 있습니까?
http://mycompany.com/restful/xyz
그렇지 않으면 HTTP 인증 오류 코드가 반환됩니다.
누구든지 도울 수 있습니까?
다음과 같이 할 수 있습니다.
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/restful/**").hasRole("ROLE_USER").and().httpBasic();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("john").password("mypass").roles("ROLE_USER");
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다