사용자 정의 사전 인증 필터를 정의해야합니다. http
태그 내에서 보안 응용 프로그램의 맥락에서
:
<custom-filter position="PRE_AUTH_FILTER" ref="preAuthTokenFilter" />
다음 (approprietly하고 해당 속성) 필터 빈을 정의
<beans:bean class="com.yourcompany.PreAuthTokenFilter"
id="preAuthTokenFilter">
<beans:property name="authenticationDetailsSource" ref="authenticationDetailsSource" />
<beans:property name="authenticationManager" ref="authenticationManager" />
<beans:property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>
</beans:bean>
은 사용자 정의 필터는
public class PreAuthTokenFilter extends GenericFilterBean {
private AuthenticationEntryPoint authenticationEntryPoint;
private AuthenticationManager authenticationManager;
private AuthenticationDetailsSource authenticationDetailsSource = new WebAuthenticationDetailsSource();
@Override
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String token = getTokenFromHeader(request);//your method
if (StringUtils.isNotEmpty(token)) {
/* get user entity from DB by token, retrieve its username and password*/
if (isUserTokenValid(/* some args */)) {
try {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
Authentication authResult = this.authenticationManager.authenticate(authRequest);
SecurityContextHolder.getContext().setAuthentication(authResult);
} catch (AuthenticationException e) {
}
}
}
chain.doFilter(request, response);
}
/*
other methods
*/
GenericFilterBean
에서 확장 만들기
비밀번호를 원하지 않거나 검색 할 수없는 경우, D는 PARAM (주)로만 이름을받을 것이다 AbstractAuthenticationToken
자신을 만들고 UsernamePasswordAuthenticationToken
대신이 기능을 사용하려면 사용자가 프로그래밍 방식으로 사용자는이 토큰에
public class PreAuthToken extends AbstractAuthenticationToken {
private final Object principal;
public PreAuthToken(Object principal) {
super(null);
super.setAuthenticated(true);
this.principal = principal;
}
@Override
public Object getCredentials() {
return "";
}
@Override
public Object getPrincipal() {
return principal;
}
}
을 기준으로 인증 하시겠습니까? –
예, 사용자 정의 UserDetailsService를 사용하여 DB에서이 토큰을 통해 사용자를로드하고 로그인/비밀번호로 로그인 한 것처럼 완전히 인증합니다. –
URL을 기반으로 사용자를 완전히 인증하는 것은 좋지 않습니다. 그 파일에 대한 액세스 권한을 제공하기 위해이 파일을 사용하고 있다면 충분할 것입니다. 그러나 일반적으로 전체 로그인으로 만 할 수있는 다른 작업을 수행 할 수 있도록 허용한다면주의해야합니다. –