linux 权限管理 改变groupid,linux的用户、组和权限管理

用户的类别分为管理员和普通用户

管理员用户即为root,普通用户分为两类,为系统用户和登录用户。系统用户仅能用于运行的服务程序,而登录用户则用于系统资源的正常使用者。

每个用户都有自己的标识,为UserID,即UID。管理员的UID为0 。系统用户在CentOS6中为1-499,CentOS7为1-999。而普通用户在CentOS6中为500+,CentOS7中为1000+。~]# id

uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

解析库,就是在username与userid之间来回转换的,若‘id = 0’,则能得到当前用户为root,对于用户账号来讲,是存放在/etc/passwd文件中的,passwd不仅能存放用户的uid,username,还能存放用户的家目录,默认的shell等等。

用户组分为管理员组和普通用户组。

组也有组的标识,即组ID,在Linux中被称为GID(GroupID),管理员的GID = 0,对于系统组,在CentOS6中为1-499,CentOS7为1-999。而普通用户组在CentOS6中为500+,CentOS7中为1000+。组名称是人来用的,而组ID是机器用的,这就用到了名称解析,来实现GID与组名之间的转换。而组名解析库就是/etc/group。

一个用户可以属于几个组,由此引出组类别,组类别以用户为核心分为基本组和额外组;根据组内容纳的用户来划分,分为私有组和公共组。私有组是与用户名相同,且只有一个此用户;公共组则包含了多个用户。

安全上下文:进程是以某个用户的身份运行,进程的操作权限取决于他所代表的用户。进程的运行者若与文件的属主相同,则以文件属主的身份访问该文件;若进程的运行者属于文件的属组,则以文件属组的身份来访问该文件,若不属于以上二者,则以其他身份来访问该文件。

管理员权限之增删查改:

用户和租的管理主要以命令运行。

组:groupadd,groupmod,groupdel

用户:useradd,usermod,userdel

认证机制:passwd,给用户指派一个密码,用户的认证信息库存储于/etc/shadow,组的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是 SpringBoot 集成 SpringSecurity 实现登录和权限管理的示例代码: 首先,我们需要在 pom.xml 中添加 SpringSecurity 的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 然后,我们需要创建一个继承了 WebSecurityConfigurerAdapter 的配置类,并且使用 @EnableWebSecurity 注解启用 SpringSecurity: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin().loginPage("/login").permitAll() .and() .logout().permitAll(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 在上面的代码中,我们通过 configure(AuthenticationManagerBuilder auth) 方法指定了使用哪个 UserDetailsService 来获取用户信息,通过 configure(HttpSecurity http) 方法配置了哪些 URL 需要哪些角色才能访问,以及登录页面和退出登录的 URL。 接下来,我们需要实现 UserDetailsService 接口,用来获取用户信息: ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("用户不存在"); } List<GrantedAuthority> authorities = new ArrayList<>(); for (Role role : user.getRoles()) { authorities.add(new SimpleGrantedAuthority(role.getName())); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities); } } ``` 在上面的代码中,我们通过 UserRepository 来获取用户信息,并且将用户的角色转换成 GrantedAuthority 对象。 最后,我们需要创建一个控制器来处理登录和退出登录的请求: ```java @Controller public class LoginController { @GetMapping("/login") public String login() { return "login"; } @GetMapping("/logout") public String logout(HttpServletRequest request, HttpServletResponse response) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { new SecurityContextLogoutHandler().logout(request, response, authentication); } return "redirect:/login?logout"; } } ``` 在上面的代码中,我们通过 @GetMapping 注解来处理登录和退出登录的请求,并且在退出登录成功后重定向到登录页面。 以上就是 SpringBoot 集成 SpringSecurity 实现登录和权限管理的示例代码,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值