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

Spring Security-简单的用户注册(不是xml配置)

湛鸿
2023-03-14

我只有2页在我的项目“/注册”和“/登录”。jsp页面来自默认的spring security登录。jsp是由我创建的。

package com.cihangirmercan.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

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

    auth.inMemoryAuthentication().withUser("cihangir").password("mercan")
            .roles("USER"); // the only user at the beginning
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/login", "/register").permitAll() // anonym can login or register
            .antMatchers("/").access("hasRole('USER')") // home page is not allowed if not user is logged in
            .and().formLogin();

    http.csrf().disable();
}
}
package com.cihangirmercan.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes("registerWarning")
public class RegisterController {

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String showRegisterPage(ModelMap model) {
        return "register";
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String handleRegisterRequest(ModelMap model, 
                                        @RequestParam String username,
                                        @RequestParam String password) {

        // i want to give this username and password ROLE_USER
        // hence user can login with spring security

        // done
        return "redirect:/login";
    }
}
<html>
<head>
<title>Register</title>
</head>
<body>
    <h1>Register</h1>
      <form action="/register" method="post" >
        <label>Username:</label>
        <input type="text" name="username" required><br><br>
        <label>Password:</label>
        <input type="password" name="password"><br><br>
        <input type="submit" value="Register">
      </form>
</body>
</html>
package com.cihangirmercan.controller;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class WelcomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showWelcomePage(ModelMap model) {
        model.put("username", getLoggedInUserName());
        return "welcome";
    }

    private String getLoggedInUserName() {
        Object principal = SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();

        if (principal instanceof UserDetails)
            return ((UserDetails) principal).getUsername();

        return principal.toString();
    }
}

welcome.jsp:

<html>
<head>
<title>Home</title>
</head>
<body>
    <h2>Home Page</h2>
    <br>
    <h4>${username} is at home.</h4>        
</body>
</html>

此外,web.xml和dispatcher-servlet和pom.xml是我的全部。

共有1个答案

尹乐邦
2023-03-14

您没有正确配置登录

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/login", "/register").permitAll() // anonym can login or register
            .antMatchers("/").access("hasRole('USER')") // home page is not allowed if not user is logged in
            .and().formLogin().loginPage("/login")
            .and()
        .logout().logoutSuccessUrl("/register");

    http.csrf().disable();
}

您已经在dispatch-xxx.xml中配置了视图解析器,如下所示

<bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
        <value>/WEB-INF/pages/</value>
      </property>
      <property name="suffix">
        <value>.jsp</value>
      </property>
    </bean>
 类似资料:
  • 新增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

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

  • 我正在尝试创建一个应用程序,我必须注册用户,我正在使用Firebase进行注册,一旦我将所有数据放入register,然后进入Firebase数据库,我发现我自己并不是在测试模式下创建数据库的注册用户。你能解释一下我可能做错了什么吗? 注册用户代码: 我正在使用实时数据库 规则Firebase:

  • 我正在尝试一个LaravelPHP项目,因为我是这个框架的新手。我要做的第一步是建立一个登记表。但是,当我单击Submit按钮时,没有给出任何错误,并且在我的users表中没有注册任何内容。 以下是到目前为止我的项目的代码: 我的用户迁移表上下功能 我的注册表控制器具有重要功能 我的路线 我的User.php模型可填充 受保护的$fillable=['姓名','性别','年龄','电子邮件','密