springboot如何訪問(wèn)windows本地和linux上的外部文件行冰?
首先編寫一個(gè)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來(lái)進(jìn)行指定想要訪問(wèn)的靜態(tài)目錄
linux服務(wù)器上以"/"表示跟目錄,windows可以用file: d: 來(lái)進(jìn)行映射
配置方式參考如下:
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)存存儲(chǔ)
auth.inMemoryAuthentication()
//設(shè)置密碼編碼器
.passwordEncoder(passwordEncoder)
//注冊(cè)用戶admin,密碼為abc悄蕾,并賦予USER和ADMIN的角色權(quán)限
.withUser("admin")
//可通過(guò)passwordEncoder.encode("abc")得到加密后的密鑰
.password("11111111111111")
//賦予角色ROLE_USER和ROLE_ADMIN
.roles("USER", "ADMIN")
//連接方法and
.and()
//注冊(cè)用戶myuser频鉴,密碼為123456,并賦予USER的角色權(quán)限
.withUser("myuser")
//可通過(guò)passwordEncoder.encode("123456")得到加密后的密鑰
.password("333333333333333333")
//賦予角色ROLE_USER
.roles("USER");
//2種方式可以處理權(quán)限認(rèn)證的過(guò)程
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
FilterSecurityInterceptor interceptor = new FilterSecurityInterceptor();
//獲取到攔截器卜范,來(lái)處理一些攔截信息
web.securityInterceptor(interceptor);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
//需要spring security保護(hù)的斷點(diǎn)
String[] endpoints = {
"auditevents", "beans", "conditions", "configprops", "env", "flyway", "httptrace",
"loggers", "liquibase", "metrics", "mappings", "scheduledtasks", "sessions", "shutdown", "threaddump"
};
//定義需要驗(yàn)證的端點(diǎn)
http.requestMatcher(EndpointRequest.to(endpoints))
//簽名登錄后
.authorizeRequests().anyRequest()
//要求登錄用戶擁有ADMIN角色
.hasRole("ADMIN")
.and()
//請(qǐng)求關(guān)閉頁(yè)面需要ROLE_ADMIN角色
.antMatcher("close").authorizeRequests().anyRequest().hasRole("ADMIN")
.and()
//啟動(dòng)http基礎(chǔ)驗(yàn)證
.httpBasic();
//處理對(duì)http請(qǐng)求的攔截認(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;
}
}