如何在springboot中实现基于用户级别的路由机制

时间:2019-05-02 12:29:12

标签: java mysql spring-boot

我正在开发具有三个用户级别(管理员,生产者,客户)的食品杂货系统。如何设置这些角色并映射到用户?

我尝试过两个实体类(用户和角色),以及某种形式的映射到用户。我正在使用MySQL数据库来处理此问题。

   //user service class
    @Service //this annotation creates this class as a
// bean and exposes it to the application context.
    public class UserService {
    //so i'm injecting the UsersRepository
    @Autowired
    private UserRepository userRepository;
    //then a method to insert a new user.
    //User here carries all the fields of the user entity/model since we      are creating a user
    public void createClient(User user){
        //but first we hash the password for security purposes.
        BCryptPasswordEncoder encoder= new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        //creating roles
        Role userRole = new Role("CLIENT");//we create a role type client
        //a user can then take a list of roles
        List<Role> roles = new ArrayList<>();//list of roles
        roles.add(userRole);//add client role type to the list
        user.setRoles(roles);//set a user to a role type
        userRepository.save(user);//save the user to the database.
    }
    public void createSupplier(User user){
        //but first we hash the password for security purposes.
        BCryptPasswordEncoder encoder= new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(user.getPassword()));
        //creating roles
        Role userRole = new Role("SUPPLIER");//we create a role type client
        //a user can then take a list of roles
        List<Role> roles = new ArrayList<>();//list of roles
        roles.add(userRole);//add client role type to the list
        user.setRoles(roles);//set a user to a role type
        userRepository.save(user);//save( save is a crudrepository method) the user to the database.
    }

//controller class with role objects to be bound with thyme-leaf fields.
  public User user;

    @GetMapping(value="newUser")
    public String newUser(Model model){
        model.addAttribute("user", new User());
        model.addAttribute("ROLE", userRoles());

        return "pages/index";
    }

    @PostMapping(value="/registerUser")
    public String createUser(@ModelAttribute(value = "user") User user){

        //if the user selects client
       userService.createClient(user);


       //if the user selects supplier
        userService.createSupplier(user);                                           

    return "pages/login";
    }
    @RequestMapping(value="/showForm",method = RequestMethod.GET)
    public ModelAndView index(){
        User user=new User();
        ModelAndView mv=new ModelAndView();
    mv.addObject("user",user);
    mv.setViewName("fragments/index");
        return mv;
   }

    private List<String> userRoles(){
        List<String> ROLE = new ArrayList<>();
        ROLE.add("CLIENT");
        ROLE.add("SUPPLIER");

        return ROLE;
    }

我希望系统使用从表单中选择的用户角色正确保存用户。系统根本不保存用户。我的完整代码为is here

0 个答案:

没有答案