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

VAADIN4Spring的ManagedSecurity:如何更新用户列表?

姜俊友
2023-03-14
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.vaadin.spring.security.annotation.EnableVaadinManagedSecurity;
import org.vaadin.spring.security.config.AuthenticationManagerConfigurer;

import com.vaadin.server.CustomizedSystemMessages;
import com.vaadin.server.SystemMessages;
import com.vaadin.server.SystemMessagesInfo;
import com.vaadin.server.SystemMessagesProvider;

import de.blume2000.kiss.hibernate.dto.User;
import de.blume2000.kiss.hibernate.services.UserService;
import de.blume2000.kiss.utils.EncryptionUtil;

@Configuration
@EnableVaadinManagedSecurity
public class SecurityConfiguration implements AuthenticationManagerConfigurer
{

    @Autowired
    UserService userService;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception
    {
        List<User> users = userService.findAll();

        if (users == null)
            return;

        for (User user : users)
        {
            String encryptedPassword = EncryptionUtil.decryptPassword(user.getPassword(), user.getSalt());
            auth.inMemoryAuthentication().withUser(user.getUsername()).password(encryptedPassword).roles(user.getRole());
        }

    }

    /**
     * Provide custom system messages to make sure the application is reloaded when the session expires.
     */
    @SuppressWarnings("serial")
    @Bean
    SystemMessagesProvider systemMessagesProvider()
    {
        return new SystemMessagesProvider()
        {
            @Override
            public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo)
            {
                CustomizedSystemMessages systemMessages = new CustomizedSystemMessages();
                systemMessages.setSessionExpiredNotificationEnabled(false);
                return systemMessages;
            }
        };
    }

}

现在,如果用户进行了登录,他可以选择编辑他的用户帐户设置。这将更改数据库中的用户对象(例如,用于登录的用户名)。现在,如果他注销,我希望应用程序重新加载userlist,这样用户就可以使用他的新用户名。这怎么可能?

问候辛奇拉

共有1个答案

彭浩穰
2023-03-14

简而言之,用DAO身份验证替换您的内存中身份验证。

请注意,在下面的示例中,UserDetailsService userService是Spring核心接口,UserRepository UserRepository用户的DAO(在示例中也称为userService userService)。

1.配置

@Configuration
public class Authorization extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private UserDetailsService userService;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
       auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserRepository userRepository;

    @Override
    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }
        return user;
    }
}
 类似资料:
  • 我有一个Java用户类,用户可以有朋友(列表 问题是,当我在代码中更改朋友并保存(用户)时,spring会添加新朋友,但不会在数据库中删除从数组列表中删除的朋友。 我试过@manytomy,@OneToMany,cascade=CascadeType。全部

  • 我正在尝试在现有列表中添加一个新项目,但无法添加。当我调试时,它显示返回为true,但最终新项没有添加到列表中。 我的示例代码如下:Employee employee1=new Employee(5001,“BOB”,financeDept.getDepartment_name());employee2=新员工(5002,“SAM”,FinanceDepartment.getDepartment_

  • 我正在尝试在现有列表中添加一个新项目,但无法添加。当我调试时,它显示返回为true,但最终新项没有添加到列表中。 我的示例代码如下:雇员雇员1=新雇员(5001,BOB,financeDept.getDepartment_name());雇员雇员2=新雇员(5002,SAM,financeDept.getDepartment_name());雇员雇员3=新雇员(5003,SAM,hrDept.ge

  • 问题内容: 我正在尝试更新所有以’agg%’和column_name=’%userid%’之类的字符串开头的表…但是即使我能够找到选择具有特定列的所有表的选项,我也看不到在线的此类示例名称和表名称我需要执行相同的操作来更新这些表,如下所示: 帮助将不胜感激。 谢谢。 问题答案: 获取您条件的更新查询 执行

  • 编辑用户信息的方法和创建新用户差不多(参见第 7 章),创建新用户的页面在 new 动作中处理,而编辑用户的页面在 edit 动作中处理;创建用户的过程在 create 动作中处理 POST 请求,编辑用户要在 update 动作中处理 PATCH 请求(旁注 3.2)。二者之间最大的区别是,任何人都可以注册,但只有当前用户才能更新自己的信息。我们可以使用第 8 章实现的认证机制,通过“事前过滤器

  • POSTGIS=“2.5.4” [扩展] PGSQL=“120” GEOS=“3.8.1-CAPI-1.13.3” PROJ=“Rel. 6.3.2, May 1st, 2020” GDAL=“GDAL 3.0.4, 2020/01/28 发布” LIBXML=“2.9.7” LIBJSON=“0.13.1” LIBPROTOBUF=“1.3.0” RASTER Java Postgis Depe