springboot访问windows本地和linux上的外部文件

springboot如何访问windows本地和linux上的外部文件?
首先编写一个WebMvcConfiguration配置文件类
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

@Value("${jarPath}")     //file:/root/gspackage/
private String path;

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**").addResourceLocations(path);
}

}

配置文件中新增静态映射,需要先把springboot默认的映射加上,用file来进行指定想要访问的静态目录
linux服务器上以"/"表示跟目录,windows可以用file: d: 来进行映射

配置方式参考如下:

spring:
mvc:
static-path-pattern: /**
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/itstyle/, file:/

springsecurity的一些记录
@EnableWebSecurity
@Configuration
public class SercurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserDetailsServiceImpl userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    super.configure(auth);
    //密码编码器
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    User user = new User();
    user.setId(1);
    user.setName("xiaoming");
    user.setPassword("123456");
    user.setRole("USER");
    manager.createUser(user);
    //使用内存存储
    auth.inMemoryAuthentication()
            //设置密码编码器
            .passwordEncoder(passwordEncoder)
            //注册用户admin,密码为abc,并赋予USER和ADMIN的角色权限
            .withUser("admin")
            //可通过passwordEncoder.encode("abc")得到加密后的密钥
            .password("11111111111111")
            //赋予角色ROLE_USER和ROLE_ADMIN
            .roles("USER", "ADMIN")
            //连接方法and
            .and()
            //注册用户myuser,密码为123456,并赋予USER的角色权限
            .withUser("myuser")
            //可通过passwordEncoder.encode("123456")得到加密后的密钥
            .password("333333333333333333")
            //赋予角色ROLE_USER
            .roles("USER");

    //2种方式可以处理权限认证的过程
    auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}


@Override
public void configure(WebSecurity web) throws Exception {
    super.configure(web);
    FilterSecurityInterceptor interceptor = new FilterSecurityInterceptor();
    //获取到拦截器,来处理一些拦截信息
    web.securityInterceptor(interceptor);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    super.configure(http);
    //需要spring security保护的断点
    String[] endpoints = {
            "auditevents", "beans", "conditions", "configprops", "env", "flyway", "httptrace",
            "loggers", "liquibase", "metrics", "mappings", "scheduledtasks", "sessions", "shutdown", "threaddump"
    };
    //定义需要验证的端点
    http.requestMatcher(EndpointRequest.to(endpoints))
            //签名登录后
            .authorizeRequests().anyRequest()
            //要求登录用户拥有ADMIN角色
            .hasRole("ADMIN")
            .and()
            //请求关闭页面需要ROLE_ADMIN角色
            .antMatcher("close").authorizeRequests().anyRequest().hasRole("ADMIN")
            .and()
            //启动http基础验证
            .httpBasic();

    //处理对http请求的拦截认证
    http.authorizeRequests()
            .antMatchers("/test").permitAll()
            .antMatchers("/login").permitAll()
            .antMatchers("/look").hasRole("user")
            .antMatchers("/**").fullyAuthenticated()
            .and()
            .formLogin().loginPage("/login")
            .failureForwardUrl("/error")
            .successForwardUrl("/index");

}

}

@Component
public class UserDetailsServiceImpl implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return new UserDetails() {
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
CopyOnWriteArrayList<GrantedAuthority> authorities = new CopyOnWriteArrayList<>();
SimpleGrantedAuthority authority = new SimpleGrantedAuthority("user");
authorities.add(authority);
return authorities;
}

        @Override
        public String getPassword() {
            return "123456";
        }

        @Override
        public String getUsername() {
            return "xiaoming";
        }

        @Override
        public boolean isAccountNonExpired() {
            return false;
        }

        @Override
        public boolean isAccountNonLocked() {
            return false;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return false;
        }

        @Override
        public boolean isEnabled() {
            return false;
        }
    };
}

}

public class User implements UserDetails {

private String name;
private String password;
private int id;
private String role;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    return null;
}

@Override
public String getPassword() {
    return null;
}

@Override
public String getUsername() {
    return null;
}

@Override
public boolean isAccountNonExpired() {
    return false;
}

@Override
public boolean isAccountNonLocked() {
    return false;
}

@Override
public boolean isCredentialsNonExpired() {
    return false;
}

@Override
public boolean isEnabled() {
    return false;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public void setPassword(String password) {
    this.password = password;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getRole() {
    return role;
}

public void setRole(String role) {
    this.role = role;
}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.创建文件夹 !/bin/sh mkdir -m 777 "%%1" 2.创建文件 !/bin/sh touch...
    BigJeffWang阅读 13,482评论 3 53
  • SpringMVC原理分析 Spring Boot学习 5、Hello World探究 1、POM文件 1、父项目...
    jack_jerry阅读 5,222评论 0 1
  • 我上辈子不光害惨了我妈,也一定对我爸做了伤天害理的事情,以至于这辈子这两口子凑成一对整治我,为了让我能够度过艰难痛...
    yeyeazi阅读 1,598评论 0 0
  • 关于手机,无论你怎么小心使用,一年换一个,或三年换两个都是很正常的了是不是?尤其是老嫌内存不够的智能手机,那些换...
    狂小烹阅读 8,218评论 48 53
  • 天有显道,厥类惟彰。 ——《周书 泰誓》 常听人说爱读哈耶克的书的人不是疯子就是傻子,颇为好奇,就读了他的代表作。...
    0a38a95e04e8阅读 2,206评论 0 0

友情链接更多精彩内容