Spring Security配置使用以及图片上传

这里使用的是Spring Security自带的默认登录

话不多说先配置xml




    

    
    
    
    
    
    
    
    
    
        
        

        

        
        

        
        

    

    
    
        
            
        
    

    
    
    
    






配置完xml在service继承他的类,重写方法

public interface IUserInfoService extends UserDetailsService{
	UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}

注意这里@Service(“userService”)里之所以要叫userService是因为配置里 配置的

@Service("userService")
public class IUserInfoServiceImpl implements IUserInfoService {
 @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserInfo userInfo = iUserInfoDao.findbyUserName(username);
        List roleList = iRoleDao.findRoleByUserId(userInfo.getId());
        userInfo.setRoleList(roleList);
        User user = new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthority(roleList));
        return user;
    }

    private Collection getAuthority(List roleList) {
        List list = new ArrayList<>();
        for (Role role:roleList){
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRname().toUpperCase()));
        }
        return list;
    }

注意这里密码一点要加密{noop}因为Spring Security默认加密数据库出来密码会在加密一次
配置完成

直接传入login.do就行了
注意使用Spring Security一定要注释掉你之前配置的过滤器和拦截器。

图片上传


       
 

后端接收

@RequestMapping("/upload.do")
    public ModelAndView upLoad(@RequestParam("myPic")MultipartFile myPic, HttpSession session) throws IOException {
        //1.文件在服务器上存储
        UUID rid = UUID.randomUUID();
        long uid = rid.getLeastSignificantBits();
        String contextPath = session.getServletContext().getRealPath("/img");
        String suffix = myPic.getOriginalFilename().substring(myPic.getOriginalFilename().lastIndexOf("."));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String path = contextPath+"\\"+df.format(new Date())+uid+suffix;
        File file = new File(path.replace("-",""));
        myPic.transferTo(file);
        //2.文件地址的回显
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("upLoad");
        modelAndView.addObject("upLoad_file_path",file.getName());
        //3.给出页面的跳转
        return modelAndView;
    }

前端配置jsp完成

你可能感兴趣的:(Spring Security配置使用以及图片上传)