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);
}

}

配置文件中新增靜態(tài)映射,需要先把springboot默認(rèn)的映射加上,用file來進行指定想要訪問的靜態(tài)目錄
linux服務(wù)器上以"/"表示跟目錄,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);
    //使用內(nèi)存存儲
    auth.inMemoryAuthentication()
            //設(shè)置密碼編碼器
            .passwordEncoder(passwordEncoder)
            //注冊用戶admin,密碼為abc,并賦予USER和ADMIN的角色權(quán)限
            .withUser("admin")
            //可通過passwordEncoder.encode("abc")得到加密后的密鑰
            .password("11111111111111")
            //賦予角色ROLE_USER和ROLE_ADMIN
            .roles("USER", "ADMIN")
            //連接方法and
            .and()
            //注冊用戶myuser,密碼為123456,并賦予USER的角色權(quán)限
            .withUser("myuser")
            //可通過passwordEncoder.encode("123456")得到加密后的密鑰
            .password("333333333333333333")
            //賦予角色ROLE_USER
            .roles("USER");

    //2種方式可以處理權(quán)限認(rèn)證的過程
    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()
            //請求關(guān)閉頁面需要ROLE_ADMIN角色
            .antMatcher("close").authorizeRequests().anyRequest().hasRole("ADMIN")
            .and()
            //啟動http基礎(chǔ)驗證
            .httpBasic();

    //處理對http請求的攔截認(rèn)證
    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;
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.創(chuàng)建文件夾 !/bin/sh mkdir -m 777 "%%1" 2.創(chuàng)建文件 !/bin/sh touch...
    BigJeffWang閱讀 10,465評論 3 53
  • SpringMVC原理分析 Spring Boot學(xué)習(xí) 5、Hello World探究 1、POM文件 1、父項目...
    jack_jerry閱讀 1,476評論 0 1
  • 我上輩子不光害慘了我媽,也一定對我爸做了傷天害理的事情,以至于這輩子這兩口子湊成一對整治我,為了讓我能夠度過艱難痛...
    yeyeazi閱讀 249評論 0 0
  • 關(guān)于手機,無論你怎么小心使用,一年換一個,或三年換兩個都是很正常的了是不是?尤其是老嫌內(nèi)存不夠的智能手機,那些換...
    狂小烹閱讀 3,154評論 48 53
  • 天有顯道,厥類惟彰。 ——《周書 泰誓》 常聽人說愛讀哈耶克的書的人不是瘋子就是傻子,頗為好奇,就讀了他的代表作。...
    0a38a95e04e8閱讀 313評論 0 0

友情鏈接更多精彩內(nèi)容