gpt4 book ai didi

java - 如何为具有 Java Spring 安全性的用户授予权限

转载 作者:行者123 更新时间:2023-11-30 06:02:47 25 4
gpt4 key购买 nike

我尝试从数据库获取一些用户并授予他们权限。这些是我的用户类字段。

private String  username;
private String email;
private String password;
private String role;

我想通过字符串角色授予权限。我试过这个:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("user1Pass"))
.authorities("ROLE_USER");
}

但我不知道如何从数据库获取用户并在configureGlobal方法中使用它们。

最佳答案

方法一:

您可以设置数据源并将查询传递给读取用户和权限,如下所示:

@Autowired
private DataSource dataSource; //autowire the data source

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery("select username, password, 1 as enabled from user where username=?")
.authoritiesByUsernameQuery("select username, authority from user_authority where username=?"); //check the for columns.
}

在这种情况下,表格看起来像这样

用户表:

CREATE TABLE user (
username VARCHAR(50) NOT NULL PRIMARY KEY,
email VARCHAR(50),
password VARCHAR(500),
activated BOOLEAN DEFAULT FALSE,
activationkey VARCHAR(50) DEFAULT NULL,
resetpasswordkey VARCHAR(50) DEFAULT NULL
);

user_authority 表:

CREATE TABLE user_authority (
username VARCHAR(50) NOT NULL,
authority VARCHAR(50) NOT NULL,
FOREIGN KEY (username) REFERENCES user (username),
FOREIGN KEY (authority) REFERENCES authority (name),
UNIQUE INDEX user_authority_idx_1 (username, authority)
);

方法 2:

如果需要,您可以为任何自定义实现实现用户详细信息服务,以下只是一个示例用户名不区分大小写:

@Component("userDetailsService")
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

private final Logger log = LoggerFactory.getLogger(UserDetailsService.class);

@Autowired
private UserRepository userRepository;

@Override
@Transactional
public UserDetails loadUserByUsername(final String login) {

log.debug("Authenticating {}", login);
String lowercaseLogin = login.toLowerCase();

User userFromDatabase = userRepository.findByUsernameCaseInsensitive(lowercaseLogin);


if (userFromDatabase == null) {
throw new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the database");
} else if (!userFromDatabase.isActivated()) {
throw new UserNotActivatedException("User " + lowercaseLogin + " is not activated");
}

Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
for (Authority authority : userFromDatabase.getAuthorities()) {
GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority.getName());
grantedAuthorities.add(grantedAuthority);
}

return new org.springframework.security.core.userdetails.User(userFromDatabase.getUsername(), userFromDatabase.getPassword(), grantedAuthorities);

}

}

和安全配置:

    @Autowired
private UserDetailsService userDetailsService;

@Bean
public PasswordEncoder passwordEncoder() {
return new StandardPasswordEncoder();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());

}

希望对你有帮助

关于java - 如何为具有 Java Spring 安全性的用户授予权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53692956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com