Spring-Security

Spring-Security,第1张

Spring-Security 一、Spring-Security初识 简介

最近做了一个ssm整合的项目,使用到了Spring-security,在我们之前的web项目在关于用户登录验证总是通过servlte中的session获取其中的user和设置user,现在我们有了框架spring就帮我把这些 *** 作都封装到一个包里面

这是spring-security的核心功能

认证 (你是谁)授权 (你能干什么)攻击防护 (防止伪造身份)
入门项目


    org.springframework.security
    spring-security-test
    test
    

首先要配置我们的web.xml



  SpringSecurity314

  
    contextConfigLocation
    classpath:spring-security.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    springSecurityFilterChain
    org.springframework.web.filter.DelegatingFilterProxy
  
  
    springSecurityFilterChain
    /*
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

同时web.xml指定了一个名为spring-security.xml的配置文件,接下来的配置就都是在这个文件中完成的。



    
    
    
    
        
        
        
        

        
        
    
    
        
            
                
                
            
        
    

如配置文件所示我们创建了2个用户user,admin

只要我们的项目一启动,Security就开始工作了

二、项目实战 连接数据库配置





关于连接数据库的配置


    
        
        
    




userService

public interface UserService extends UserDetailsService {
	//啥也不用干
}

user实现类

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserInfo userinfo = null;
        userinfo = userDao.findByUsername(s);
        //处理的对象封装成UserDetails
//        User user = new User(userinfo.getUsername(), "{noop}" +userinfo.getPassword(), getAuthority(userinfo.getRoles()));
        User user = new User(userinfo.getUsername(), userinfo.getPassword(), userinfo.getStatus() == 0 ? false : true, true, true, true, getAuthority(userinfo.getRoles()));
        return user;
    }

    private List getAuthority(List roles) {
        List authorities = new ArrayList<>();
        for (Role role : roles) {
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return authorities;
    }
}    

loadUserByUsername根据id查询user返回给Security处理,下面这个函数是我们根据数据库中存储的role格式进行修改

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WZgIsicZ-1642407664314)(C:Users86157AppDataLocalTemp1642398962529.png)]

    
    

由于配置了加密方式,所以我们的密码不用加{noop}


这是关于退出的代码Security已经帮我们做好了

权限管理 jsr250配置

pom.xml文件


    javax.annotation
    jsr250-api
    1.0

首先我们在配置文件中简介配置开始权限控制


然后在我们的servlet中添加注解

我们是通过这个注解实现的@RolesAllowed({“ADMIN”})

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    //给用添加id
    @RolesAllowed({"ADMIN"})
    @RequestMapping("/addRoleToUser.do")
    public String addRoleToUser(@RequestParam(name = "userId",required = true) String userId,@RequestParam( name = "ids",required = true) String[] roleIds){
        userService.addRoleToUser(userId,roleIds);
        return "redirect:findAll.do";
    }
}    

这样就只有具有ADMIN权限的用户才能进行添加 *** 作

@PermitAll表示允许所有的角色进行访问,也就是说不进行权限控制

@DenyAll是和PermitAll相反的,表示无论什么角色都不能访问

除了这个配置还有另一个

secured配置

通过@secured


@Secured注解标注的方法进行权限控制的支持,其值默认为disabled。

@Secured(“ROLE_TELLER”) 这样配置

Spel表达式配置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yQGiwrvG-1642407664315)(D:Ajava技能树SSM整合SSM整合案例企业权限管理系统day05资料4.表达式使用介绍.bmp)]

页面端标签控制权限

在jsp页面中我们可以使用spring security提供的权限标签来进行权限控制

maven导入

       
           org.springframework.security
           spring-security-taglibs
           ${spring.security.version}
       

页面导入

<%@taglib uri="http://www.springframework.org/security/tags" prefix="security"%>

  • 用户管理
  • 
        
        
    
    

    把use-expressions改为true

           
    
           
    

    security:intercept-url pattern="/**" access=“ROLE_USER,ROLE_ADMIN”/>

    把use-expressions改为true
    
    
       
    
       
    
    					
    										
    
    
    					

    欢迎分享,转载请注明来源:内存溢出

    原文地址: https://outofmemory.cn/zaji/5707461.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-12-17
    下一篇 2022-12-17

    发表评论

    登录后才能评论

    评论列表(0条)

    保存