在主体中更新用户的名字和姓氏 [英] Update User's first name and last name in principal

查看:31
本文介绍了在主体中更新用户的名字和姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新用户的信息,例如名字姓氏,并且我在欢迎信息的所有页面中都获得了名字和姓氏>.

I am updating user's information like first name and last name and I am getting first name and last name in all the pages for welcome message.

我有两个控制器,一个用于ajax 请求映射,另一个用于普通 请求映射.

I have two controllers one for ajax request mapping and the other for normal request mapping.

普通请求映射控制器有这个方法.在这个控制器中,所有页面导航都存在,还有一些不是 ajax 调用的请求映射

Normal request mapping controller have this method. In this controller all page navigation is present and some request mapping which are not ajax calls

private String getPrincipalDisplay() {
    GreenBusUser user = null;
            String userName = "";
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            if (principal instanceof UserDetails) {
                user = (GreenBusUser) principal;
                userName = user.getFirstName() + " " + user.getLastName();
            } else {
                userName = "";
            }
            return userName;
}

这就是我通过返回此函数的字符串在每个页面上获取用户名的方式,我将其添加到 ModelMap 对象中.

This is how I am getting the username on every page by return string of this function I am adding it in ModelMap object.

当我更新用户信息时,我正在做 ajax 请求映射.

When I update user's information I am doing in ajax request mapping.

@RequestMapping(value = "/restify/updateUserData",  method = RequestMethod.PUT, headers = "Accept=application/json")
    public ServiceResponse forgotPassword(@RequestBody Object user)
    {   
        //logger.debug("getting response");
        return setDataPut("http://localhost:7020/forgotPassword",user);
    } 

user 是具有 json 数据的 Object 类型.现在我如何从对象中检索数据并更新我的名字和姓氏.

user is an Object type which has json data. Now how do I retrieve data from object and update my first name and last name in principal.

这是我的GreenBusUser

public class GreenBusUser  implements UserDetails
{
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private Collection<? extends GrantedAuthority> grantedAuthorities;

    private String firstName;

    private String lastName;

    public GreenBusUser(String username,String password,Collection<? extends GrantedAuthority>  authorities,String firstName, String lastName)
    {
        this.username = username;
        this.password = password;
        this.grantedAuthorities = authorities;
        this.firstName=firstName;
        this.lastName=lastName;

        this.grantedAuthorities.stream().forEach(System.out::println);

    }

    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        return grantedAuthorities;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPassword()
    {
        return password;
    }

    public String getUsername()
    {
        return username;
    }

    public boolean isAccountNonExpired()
    {
        return true;
    }

    public boolean isAccountNonLocked()
    {
        return true;
    }

    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    public boolean isEnabled()
    {
        return true;
    }
}

更新:::::

我已更新您的代码并将您的部分答案应用到我的代码中,但我仍然遇到了问题

I have updated your code and applied some part of your answer into mine but still I ran into a problem

@RequestMapping(value="/updateUser",method=RequestMethod.GET)
    public String updateUser(ModelMap model) {

        UserInfo user = getUserObject();
        GreenBusUser newGreenBususer = null;
        List<User> list = new ArrayList<User>();


        list = FetchDataService.fetchDataUser("http://localhost:8060/GetuserbyUserName?username=" + getPrincipal(), user.getUsername(), user.getPassword());
        logger.debug("new user list ----->>>"+list.size());
        User newuser=(User)list.get(0);
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
                   SecurityContextHolder.getContext().getAuthentication().getPrincipal(), SecurityContextHolder.getContext().getAuthentication().getCredentials());

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        newGreenBususer=(GreenBusUser)principal;

        logger.debug("newGreenBususerDetails---->>>"+newGreenBususer.toString());

        newGreenBususer.setFirstName(newuser.getFirstName());
        newGreenBususer.setLastName(newuser.getLastName());

        if(newGreenBususer.getFirstName()!=null) {
            logger.debug("got my first name");
        }
        if(newGreenBususer.getLastName()!=null) {
            logger.debug("got my last name");
        }

        auth.setDetails(newGreenBususer);       
        SecurityContext context = SecurityContextHolder.getContext();
        context.setAuthentication(auth);
        SecurityContextHolder.setContext(context);
        model.addAttribute("user", getPrincipalDisplay());
        model.addAttribute("userData", list);
        model.addAttribute("check", true);
        return "GreenBus_updateProfile_User";
    }

首先它将名字和姓氏设置为GreenBusUser,然后当我重新加载页面时出现setDetails方法,它显示未找到用户当我在此方法的顶部调用 getUserObject() 方法时.

At first it sets the firstname and lastname to GreenBusUser and then there is setDetails method when I reload the page it says No user found when I am calling getUserObject() method at the top of this method.

private X2CUser getUserObject() {
        X2CUser userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            userName = ((X2CUser) principal);
        } else {
            logger.info("No user found");

        }
        return userName;
    }

推荐答案

我终于解决了我的问题,尽管我后来在 UPDATE 部分的问题部分添加了一些代码.

I have finally resolved my problem though I have later added some code in my question part in UPDATE section.

 Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        newGreenBususer=(GreenBusUser)principal;


        newGreenBususer.setFirstName(newuser.getFirstName());
        newGreenBususer.setLastName(newuser.getLastName());

是的,这一切都需要完成.

Yes that's all need to be done.

这部分--->>

    auth.setDetails(newGreenBususer);       
    SecurityContext context = SecurityContextHolder.getContext();
    context.setAuthentication(auth);
    SecurityContextHolder.setContext(context);

设置新上下文使安全性指向 null 当我重新加载时仍然不清楚,因为我在重新加载之前之前设置了详细信息,所以它就像我得到了新上下文 但我已经设置了新的用户详细信息.

set new context making security pointing to null when I reload still not clear because I am setting the details before reload so its like I get new context but I have set the new user details.

虽然我终于解决了我的问题,但如果有人能解释为什么会这样,那么我会接受他/她的回答.

非常感谢您的支持.继续学习!

这篇关于在主体中更新用户的名字和姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆