SpringおよびSpring Security 3.1で特定のユーザー名でWeb訪問者をプログラムでログインさせる適切な方法は何ですか? 2.5でやっていたやり方が少し変わったようです。私は今これを行うためのはるかに良い方法があると確信しています.
基本的に、新しいユーザーを作成するときは、同時にログインする必要があります。
SpringおよびSpring Security 3.1で特定のユーザー名でWeb訪問者をプログラムでログインさせる適切な方法は何ですか? 2.5でやっていたやり方が少し変わったようです。私は今これを行うためのはるかに良い方法があると確信しています.
基本的に、新しいユーザーを作成するときは、同時にログインする必要があります。
Authentication
(通常は) を作成してUsernamePasswordAuthenticationToken
から呼び出します
SecurityContextHolder.getContext().setAuthentication(authentication)
この 3 行のコードで作業が完了します。
Authentication request = new UsernamePasswordAuthenticationToken( username, password );
Authentication result = authenticationManager.authenticate( request );
SecurityContextHolder.getContext().setAuthentication( result );
テスト目的でこれを行うことに興味がある場合は、次のようにすることができます。
UserDetails user = _userService.loadUserByUsername(username);
TestingAuthenticationToken token = new TestingAuthenticationToken(user,null);
SecurityContextHolder.getContext().setAuthentication(token);
ユーザーサービスは UserDetailsService を実装するものです
custom UsernamePasswordAuthenticationFilter
Spring の を拡張した を書くことができますUsernamePasswordAuthenticationFilter
。
コードは次のとおりです。
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException, ServletException {
super.successfulAuthentication(request, response, authResult);
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authResult;
WebAuthenticationDetails details = (WebAuthenticationDetails) token.getDetails();
String address = details.getRemoteAddress();
User user = (User) authResult.getPrincipal();
String userName = user.getUsername();
System.out.println("Successful login from remote address: " + address + " by username: "+ userName);
}
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
super.unsuccessfulAuthentication(request, response, failed);
UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) failed.getAuthentication();
WebAuthenticationDetails details = (WebAuthenticationDetails) token.getDetails();
String address = details.getRemoteAddress();
System.out.println("Failed login try from remote address: " + address);
}
}