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

Spring Security:注册新用户的公共API

赵经国
2023-03-14

我有一个用例,我想将HTTP POSTauserName作为JSON到我的服务,然后服务应该注册一个新用户(使用给定的userName和自动生成的密码)。

数据

{
"userName": "Sia"
}

我正在使用Spring Security,我面临的问题是:

每当我尝试HTTP POST用户名时,服务已经要求身份验证(用户名和密码)。这不是我想要的原因。我希望注册 API 是完全公开的。这意味着每个人(未经授权)都可以HTTP POST一个新的用户名,从而“打开帐户”。

我不确定我怎样才能达到想要的行为。服务的一部分应该是公共的(就像上面描述的创建一个新用户一样),而有些部分确实应该需要认证(在描述的公共< code>POST过程中创建的用户的认证)。有什么建议吗?

pom.xml

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  </dependencies>

UserController.java

@Autowired
private UserService service;    

@RequestMapping(method = RequestMethod.POST, value = "/user")
public void register(@PathVariable String userName) {
    System.err.println("!!!!!!!!!!!"); // this line never gets executed

    service.save(userName);
}

UserService.java

public void save(String userName) {     
    String password = pwGenerator.generate();       
    repository.save(new User(userName, password));
}

共有2个答案

凤安然
2023-03-14

您可以在您的安全配置中拥有一个所有人都允许的URL:

.antMatchers("/user").permitAll()

如果您遇到CSRF保护问题,您可以使用

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

    // Build the request matcher for CSFR protection
    RequestMatcher csrfRequestMatcher = new RequestMatcher() {

        // Always allow the HTTP GET method

        // Disable CSFR protection on the following urls:
        private AntPathRequestMatcher[] requestMatchers = {
                new AntPathRequestMatcher("/user") };

        @Override
        public boolean matches(HttpServletRequest request) {

            if (request.getMethod().matches(GET_METHOD_REGEX))
                return false;

            for (AntPathRequestMatcher rm : requestMatchers) {
                if (rm.matches(request)) {
                    return false;
                }
            }
            return true;
        } // method matches

    }; // new RequestMatcher

并在 http 配置中使用上述内容。

http.csrf().requireCsrfProtectionMatcher(csrfRequestMatcher)
裴良弼
2023-03-14

您可以尝试下面的代码,以允许/user通过spring security发布请求。

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

        http.authorizeRequests()
             .antMatchers(HttpMethod.POST, "/user") 
               .permitAll().anyRequest().authenticated();

    }
}   
 类似资料:
  • 我试图为我的网络应用程序建立一个登录系统,但我无法让Passport工作。该应用程序是作为REST API构建的,因此用户应该能够使用电子邮件和密码注册,之后他们应该能够使用这些凭据登录(所以我认为他们需要在登录凭据时从Passport接收访问令牌是正确的)。 我想我可以只向“注册”路径发送一个JSON帖子来注册一个新用户,然后向“登录”路径发送一个帖子来将访问令牌返回到客户端,但据我所知,没有这

  • 新增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

  • 如何注册一个新用户,而不通过管理员,keycloak提供了一个界面来注册一个新的用户URL,以便直接在浏览器中注册新用户: http://localhost:8080/auth/realms/myrealm/login-行动/注册?客户id=clientid 但我找不到注册的rest API的endpoint。我成功地使用admin rest API添加了一个用户,但我想注册一个新用户,但不提及a

  • Registering users(用户注册) Loopback 的内置User Model,提供了注册,确认 Email 地址. 同时通过 loopback-component-passport 模块能够整合 google, Facebook ,github 等第三方登录. 通过内置 User Model 注册用户 创建一个新用户 添加注册限制 验证 email 地址 使用第三方注册 创建一个新