Java Spring启动-使用inMemoryAuthentication
我已经使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包生成了一个Spring引导web应用程序 使用的技术: 弹簧靴2.0.0.M6,Java 8,maven 我有这个安全配置文件,以便在开发阶段使用内存中的身份验证 在我的应用程序中。我有一个用户域对象:Java Spring启动-使用inMemoryAuthentication,java,spring,spring-mvc,spring-boot,spring-security,Java,Spring,Spring Mvc,Spring Boot,Spring Security,我已经使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎和作为可执行JAR文件的包生成了一个Spring引导web应用程序 使用的技术: 弹簧靴2.0.0.M6,Java 8,maven 我有这个安全配置文件,以便在开发阶段使用内存中的身份验证 在我的应用程序中。我有一个用户域对象: import org.springframework.security.core.userdetails.UserDetails; public class User implements
import org.springframework.security.core.userdetails.UserDetails;
public class User implements Serializable, UserDetails {
..
}
在我的安全配置类中
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser(User
.withDefaultPasswordEncoder()
.username(DEV_USER)
.password(DEV_PWD)
.roles("ADMIN").build());
}
但是当我从SecurityContextHolder获取用户时:
SecurityContextHolder.getContext().
getAuthentication().getPrincipal()
我有一个错误:
org.springframework.security.core.userdetails.User cannot be cast to com.iberia.domain.backend.User
但我找不到构造UserDetails对象的方法:
UserDetails
.withDefaultPasswordEncoder()
.username(DEV_USER)
.password(DEV_PWD)
.roles("ADMIN").build()
您可以使用
org.springframework.security.core.userdetails.User
String username = DEV_USER;
String encodedPassword = //encoded password
List<SimpleGrantedAuthority> authList = getAuthorities("ADMIN");
User user = new User(username, encodedPassword, authList);
String username=DEV\u USER;
字符串encodedPassword=//编码密码
List authList=getAuthorities(“ADMIN”);
User User=新用户(用户名、encodedPassword、authList);
具有:
User==>org.springframework.security.core.userdetails.User
String username = DEV_USER;
String encodedPassword = //encoded password
List<SimpleGrantedAuthority> authList = getAuthorities("ADMIN");
User user = new User(username, encodedPassword, authList);
SimpleGrantedAuthority==>org.springframework.security.core.authority.SimpleGrantedAuthority
然后您可以将用户对象强制转换为
org.springframework.security.core.userdetails.userdetails
Object您可以使用org.springframework.security.core.userdetails.user
String username = DEV_USER;
String encodedPassword = //encoded password
List<SimpleGrantedAuthority> authList = getAuthorities("ADMIN");
User user = new User(username, encodedPassword, authList);
String username=DEV\u USER;
字符串encodedPassword=//编码密码
List authList=getAuthorities(“ADMIN”);
User User=新用户(用户名、encodedPassword、authList);
具有:
User==>org.springframework.security.core.userdetails.User
String username = DEV_USER;
String encodedPassword = //encoded password
List<SimpleGrantedAuthority> authList = getAuthorities("ADMIN");
User user = new User(username, encodedPassword, authList);
SimpleGrantedAuthority==>org.springframework.security.core.authority.SimpleGrantedAuthority
然后可以将用户对象强制转换为
org.springframework.security.core.userdetails.userdetails
Object是否尝试过Object principal=SecurityContextHolder.getContext().getAuthentication().getPrincipal();如果(principal instanceof UserDetails)用户名=((UserDetails)principal).getUsername()?如果您需要使用自定义用户,则必须实现您自己的UserDetailsService
,如其他问题中所述。如果不需要自定义用户,可以使用Spring Security的用户。是否尝试过Object principal=SecurityContextHolder.getContext().getAuthentication().getPrincipal();如果(principal instanceof UserDetails)用户名=((UserDetails)principal).getUsername()?如果您需要使用自定义用户,则必须实现您自己的UserDetailsService
,如其他问题中所述。如果不需要自定义用户,可以使用Spring Security的用户。