12

SpringおよびSpring Security 3.1で特定のユーザー名でWeb訪問者をプログラムでログインさせる適切な方法は何ですか? 2.5でやっていたやり方が少し変わったようです。私は今これを行うためのはるかに良い方法があると確信しています.

基本的に、新しいユーザーを作成するときは、同時にログインする必要があります。

4

4 に答える 4

19

Authentication(通常は) を作成してUsernamePasswordAuthenticationTokenから呼び出します

SecurityContextHolder.getContext().setAuthentication(authentication)
于 2011-09-30T21:43:43.560 に答える
4

この 3 行のコードで作業が完了します。

        Authentication request = new UsernamePasswordAuthenticationToken( username, password );
    Authentication result = authenticationManager.authenticate( request );
    SecurityContextHolder.getContext().setAuthentication( result );
于 2012-12-18T22:03:08.497 に答える
2

テスト目的でこれを行うことに興味がある場合は、次のようにすることができます。

    UserDetails user = _userService.loadUserByUsername(username);
    TestingAuthenticationToken token = new TestingAuthenticationToken(user,null);
    SecurityContextHolder.getContext().setAuthentication(token);

ユーザーサービスは UserDetailsS​​ervice を実装するものです

于 2014-06-19T09:02:10.717 に答える
0

custom UsernamePasswordAuthenticationFilterSpring の を拡張した を書くことができます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);
    }
}
于 2011-09-30T21:48:52.457 に答える