- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个简单的 spring web mvc 应用程序,但有一个问题。身份验证后,我尝试获取身份验证对象,但由于某种原因,它的凭据为空。
在这个项目中,我有一个自定义的 AuthenticationProvider,如下所示:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@PostConstruct
public void init() {
roleService.AddStandardRolesIfNeeded();
userService.AddUserWithAdminRoleIfNotExists("a");
}
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Object credentials = authentication.getCredentials();
if(!(credentials instanceof String)) {
return null;
}
String username = authentication.getName();
String password = credentials.toString(); //password isn't null here
User user = userService.findByUsernameAndPassword(username, password);
if(user == null) {
throw new BadCredentialsException("Authentication failed for " + username);
}
List<GrantedAuthority> authorities = new ArrayList<>();
for(Role role : user.getRoles()) {
authorities.add(new SimpleGrantedAuthority(role.getName()));
}
Authentication auth = new
UsernamePasswordAuthenticationToken(username, password, authorities);
return auth;
}
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
我感兴趣的是它是在 Spring Security 中有意完成的还是我遗漏了一些东西。
最佳答案
由于身份验证成功后凭据将被删除,因此您必须将 eraseCredentials(false)
添加到 AuthenticationManagerBuilder
配置中。就像下面的代码:
@Autowired
private CustomAuthenticationProvider customAuthProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider).eraseCredentials(false);
}
这已经晚了,但我认为根据问题这是正确的答案。
关于java - SecurityContextHolder.getContext().getAuthentication().getCredentials() 身份验证后返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53496272/
我们将应用程序从 Spring & Hibernate 3.x 升级到 4.x。问题是 AuthenticationException.getAuthentication() 现在已被弃用。任何人都可
我在我的 Spring Boot 项目中处理安全层时遇到了以下问题: SecurityContextHolder.getContext().getAuthentication() 此代码返回: 匿名用
当我尝试模拟时 SecurityContextHolder.getContext().getAuthentication().getCredentials() 要获得 UserDetails 的 Ma
我正在尝试从 Spring Boot + AngularJS 应用程序返回当前登录的用户,但是 SecurityContextHolder.getContext().getAuthentication
我正在实现 MyUsernamePasswordAuthenticationFilter 扩展默认过滤器。 我没有在其中做任何事情。我只是想在这里获取请求以使其变得简单。 public class M
我在我的项目中使用 Spring 3,现在升级到 Spring 4.0.3.RELEASE。现在在使用 AuthenticationException.getAuthentication() 时,它说
我想使用以下代码从 spring Security 中手动绕过用户: User localeUser = new User(); UsernamePasswordAuthenticationToken
我创建了一个简单的 spring web mvc 应用程序,但有一个问题。身份验证后,我尝试获取身份验证对象,但由于某种原因,它的凭据为空。 在这个项目中,我有一个自定义的 Authenticatio
用户注册成功后,我尝试将用户重定向到登录后页面并尝试从 SecurityContextHolder.getContext().getAuthentication(); 获取 Activity 用户;返
我已经实现了一个映射到特定 URL "/partner/login" 的自定义安全机制,我在其中使用我自己的子类 AbstractAuthenticationProcessingFilter 生成子类
我已尝试实现此 question. 中提供的解决方案 但是,它们都不适合我。在我的抽象基 Controller 中,我有以下方法(我也尝试在 helper 类中使用静态方法,但同样的问题): publ
我有一个典型的 Spring MVC 在 Tomcat 上运行。将系统切换为在 HTTPS 上运行(在纯 HTTP 下一切正常)后,登录停止工作。原因是Spring的SecurityContextHo
我是一名优秀的程序员,十分优秀!