相关疑难解决方法(0)

如何在不知道密码的情况下使用Spring Security自动登录用户?

我的应用程序使用Spring Security,我的客户端需要:

  • 用户能够在注册后自动登录.
  • 管理员以任何用户身份登录而不知道他们的密码.

所以我需要弄清楚如何在不知道密码的情况下自动登录任何用户.

如何使用Spring Security实现这一目标.

java security authentication spring

7
推荐指数
1
解决办法
1万
查看次数

用户授权的权限始终是:ROLE_ANONYMOUS?

我在注册后使用以下方法进行编程登录

private void autoLogin(User user,
            HttpServletRequest request)
    {

GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
                "ROLE_ADMIN") };

        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
                user.getUsername(), user.getPassword(),grantedAuthorities);

        // generate session if one doesn't exist
        request.getSession();

        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authenticatedUser = authenticationManager.authenticate(token);

        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    }
Run Code Online (Sandbox Code Playgroud)

用户是经过身份验证但总是有ROLE_ANONYMOUS我不知道为什么?有任何想法吗 ?

spring spring-mvc spring-security

6
推荐指数
1
解决办法
2万
查看次数

Spring boot:注册成功后尝试自动登录时 java.lang.StackOverflowError:null

我需要在注册成功后自动登录。我关注了这些stackoverflow 帖子文章java.lang.StackOverflowError : null,但在通过 Postman 测试我的代码时得到了。

控制器类:

@RestController
public class RegistrationController {

    @Autowired
    private UserService userService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @PostMapping("/api/user/registration")
    public ResponseEntity registerNewUserAccount(
            @RequestBody @Valid RegistrationDto userDto, HttpServletRequest request){

        userService.save(userDto);
        authenticateUser(userDto, request);

        return ResponseEntity.ok().build();
    }

    private void authenticateUser(RegistrationDto userDto, HttpServletRequest request){
        String username = userDto.getEmailAddress();
        String password = userDto.getPassword();

        UsernamePasswordAuthenticationToken token =
                new UsernamePasswordAuthenticationToken(username, password);

        request.getSession();
        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authenticatedUser =
                authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    }
}
Run Code Online (Sandbox Code Playgroud)

安全配置类:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { …
Run Code Online (Sandbox Code Playgroud)

java spring-security spring-boot

5
推荐指数
1
解决办法
2369
查看次数