当前位置: 首页 > 知识库问答 >
问题:

将用户注册到存储库Spring Security

储仲渊
2023-03-14

我有一个运行时账户库(我没有使用数据库),我希望能够在运行时注册新账户。我有一个专门的控制器。

除此之外,我有一个需要身份验证的控制器。请注意,我没有任何图形形式,只有API,因此身份验证必须来自HTTP请求。

RegisterAccountController.java:保存到存储库并返回JSON密码

@Autowired
private AccountService service;

@RequestMapping(method = RequestMethod.POST, value = "/account", produces = "application/json")
public String create(@RequestBody String accountId) {
        String password = service.generatePassword();
        service.save(accountId, password);

        return password;
    }
}

AccountService。java:将用户保存到本地运行时存储库

@Autowired
private AccountRepository repository;

@Autowired
private PasswordGenerator pwGenerator;

public void save(String accountId, String password) {
    repository.save(new Account(accountId, password));
}

public String generatePassword() {
    // random password is returned
    return pwGenerator.generate();
}

AccountRepository.java

// accountId --> account
private Map<String, Account> accounts = new HashMap<>();


public Account findById(String accountId) {
    return accounts.get(accountId);
}


public void save(Account account) {     
    accounts.put(account.getAccountId(), account);
}

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired  
    private AccountDetailsService accountDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(accountDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // permit public POST to '/account' endpoint
        http.authorizeRequests()
                            .antMatchers("/account").permitAll() // allow everyone to open account
                            .anyRequest().fullyAuthenticated() // all other endpoints authenticated
                            .and().httpBasic();

        http.csrf().disable();
    }
}

AccountDetailsService.java

@Service
public class AccountDetailsService implements UserDetailsService {

    @Autowired
    private final AccountRepository repository;

    @Override
    public UserDetails loadUserByUsername(String accountId) throws UsernameNotFoundException {
        Account account = repository.findById(accountId);
        return new User(account.getId(), account.getPassword(), Collections.emptySet()); // no granted authorities
    }
}

AuthorizedController.java:只有授权的帐户才能访问它

@RequestMapping(method = RequestMethod.POST, value = "/hello", produces = "application/json")
public String hello() {
    return "Hello, authenticated!";
}

现在的问题是,每当我在我的注册表中注册一个新用户,然后尝试点击AuthorizedController,我就无法使用注册用户的凭据登录。

例如,用户“Joe”已注册,并获得随机密码“12345678”。现在,我用凭证“Joe”、“12345678”点击了“/hello”endpoint,我遇到了未经授权的异常,好像应用程序的身份验证部分没有从帐户存储库中获取信息。

请尝试提供一些建议。

共有1个答案

孟永望
2023-03-14

我认为你不断得到未授权的异常的原因是在你的AccountDetailsService.java中你没有指定它的许可。您的代码必须类似于

@Service
public class AccountDetailsService implements UserDetailsService {
@Autowired
private final AccountRepository repository;

@Override
public UserDetails loadUserByUsername(String accountId) throws UsernameNotFoundException {
    Account account = repository.findById(accountId);
    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    return new User(account.getId(), account.getPassword(), 
   authorities);
}
}
 类似资料:
  • 我对docker注册表和存储库之间的区别感到困惑。Docker文档似乎可以互换使用这两个词。此外,存储库有时也被称为映像,例如来自其文档的以下内容: 为了将存储库推送到它的注册表,您需要命名一个映像,或者将您的容器提交给一个命名映像,就像我们在这里看到的那样。 现在您可以将此存储库推送到由其名称或标记指定的注册表中。 如何将存储库推进到注册表?你不是在把映像推到存储库吗?

  • 问题内容: 我不希望新用户能够注册。因此,在Jenkin的配置中,我使用Jenkin自己的用户数据库禁用了“允许用户注册”。 但是,现在如何手动添加用户?另外,是否有我应该照顾的默认管理员用户? 问题答案: 在“管理詹金斯”中有“创建用户”。

  • 新增ISV有两种方式 方式1 启动sop-admin,在admin后台ISV管理添加 方式2 启动sop-website-server,用户访问自主注册

  • 接口说明 注册用户 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /usercenter/api/register/v1.0.0/registerUser 是否需要登录 否 请求字段说明 参数 类型 请求类型 是否必须 说明 loginName string formData 是 用户登录 mobile string formDa

  • 接口说明 注册用户 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 如开启https功能,请求地址的协议应改为https,如:https://www.example.com/wish3dearth/api/access/v1.0.0/getLicenseInfo API地址 POST /usercenter/api/register/v1.0.0/reg

  • 我正在使用匕首2,我有一个定义如下的: 这是FYViewModel的: 但是,变量始终为空。 如何使用匕首2将Repositroy注入我的ViewModel?