springboot 訪問上傳頁面因csrf出現403的問題
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
MemDetailsService memDetailsService;
@Autowired
SimpleLoginSuccessHandler simpleLoginSuccessHandler;
@Override
//WebSecurity:For example, if you wish to ignore certain requests.
//用於配置類似防火牆,放行某些URL。
public void configure(WebSecurity web) throws Exception {
// 設定不攔截規則
//web.ignoring().antMatchers("/js/**", "/css/**", "/images/**", "/**/favicon.ico", "/swagger*/**", "/image/**", "/webjars/**","/v2/**");
web.ignoring().antMatchers("/js/**", "/css/**", "/images/**", "/**/favicon.ico", "/image/**");
}
@Override
//HttpSecurity:一般用它來具體控制許可權,角色,url等安全的東西。
protected void configure(HttpSecurity http) throws Exception {
// 設定CSRF規則
http.csrf().requireCsrfProtectionMatcher(new SimpleCsrfSecurityRequestMatcher()).and().
// 設定攔截規則
authorizeRequests()
.antMatchers("/api/**", "/index", "/updateIndex.html", "/browserIndex.html", "/policy-zcff.html", "/policy-hydj.html", "/policy-jf.html", "/policy-card.html", "/faq.html", "/cm/satCm01Init", "/cm/satCm01List", "/faq/satFaq01", "/logout", "/loginSso", "/bulterservice.html", "/verifySso").permitAll()
.antMatchers("/autoconfig/**", "/beans/**", "/configprops/**", "/dump/**", "/env/**", "/health/**", "/info/**", "/metrics/**", "/mappings/**", "/shutdown/**", "/trace/**").access("hasRole('ADMIN')")
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").usernameParameter("saID").passwordParameter("password").permitAll().defaultSuccessUrl("/home", true).failureForwardUrl("/index").successHandler(simpleLoginSuccessHandler)
.and().logout().logoutUrl("/logout").logoutSuccessUrl("/index")
.and().exceptionHandling().accessDeniedPage("/logout")
.and().sessionManagement().maximumSessions(1).expiredUrl("/index");
}
@Override
//用於配置Authentication,比如LDAP, Database連線,以及使用者和角色的查詢方法。
public void configure(AuthenticationManagerBuilder auth) throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
daoAuthenticationProvider.setUserDetailsService(memDetailsService);
auth.authenticationProvider(daoAuthenticationProvider);
//auth.userDetailsService(memDetailsService);
//.passwordEncoder(new BCryptPasswordEncoder())
}
}
要解決403訪問許可權問題 必須加http.csrf().requireCsrfProtectionMatcher(new SimpleCsrfSecurityRequestMatcher()
要把上傳頁面URL過濾掉才能解決403
SimpleCsrfSecurityRequestMatcher具體實現
public class SimpleCsrfSecurityRequestMatcher implements RequestMatcher {
private Logger logger = LoggerFactory.getLogger(this.getClass());
private Pattern allowedMethods = Pattern
.compile("^(GET|HEAD|TRACE|OPTIONS)$");
@Override
public boolean matches(HttpServletRequest request) {
if (execludeUrls.size() > 0) {
String servletPath = request.getServletPath();
for (String url : execludeUrls) {
if (servletPath.contains(url)) {
logger.debug("SimpleCsrfSecurityRequestMatcher排除的url:" + servletPath);
return false;
}
}
}
return !allowedMethods.matcher(request.getMethod()).matches();
}
/**
* 需要排除的url列表
*/
private final List execludeUrls = new ArrayList() {{
add("/upload");
add("/upload/uploadActivateAttachment");
add("/buy02");
add("/buy02/uploadActivationSel");
}};
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/750077/viewspace-2141159/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 域名訪問和ip訪問引起的http 403問題HTTP
- Docker構建的tomcat工程上傳檔案,訪問報403許可權問題DockerTomcat
- IP頁面訪問域名介面問題
- springboot無妨訪問html頁面Spring BootHTML
- 解決圖片訪問403 Forbidden問題ORB
- 無法訪問請求的頁面,因為該頁的相關配置資料無效問題的解決
- docker 部署 Laravel 上線訪問 403 記錄DockerLaravel
- 支援合作伙伴的應用問題:頁面不能訪問,因為沒設定GBK編碼
- springboot中靜態頁面無法訪問及return無法重定向問題Spring Boot
- 有關laravel 上傳圖片訪問404的問題Laravel
- 頁面傳值地址爛亂碼問題
- Express 配置HTML頁面訪問ExpressHTML
- webuploader在同頁面初始化多個上傳元件問題Web元件
- JAVA訪問雲資料mysql出現問題JavaMySql
- LNMP的403問題總結LNMP
- laravel 500頁面不能正常訪問Laravel
- Springboot使用avue拖動上傳遇到的問題Spring BootVue
- msmq在Internet上訪問的問題(只能傳送,不能接收)MQ
- 訊息通道路由出現 403 問題的最好解決辦法路由
- 解決:LNMP架構下訪問php頁面出現500錯誤薦LNMP架構PHP
- JSP頁面中新增完一個專案後,重新整理頁面出現的問題JS
- 傳送郵件出現問題
- dubbo繼承springboot出現的問題繼承Spring Boot
- Nginx採用https加密訪問後出現的問題NginxHTTP加密
- JavaScript子頁面和父頁面相互訪問JavaScript
- nginx設定預設訪問頁面Nginx
- Nginx 配置訪問 swagger 頁面NginxSwagger
- 新來的前端小姐姐問:Vue路由history模式重新整理頁面出現404問題前端Vue路由模式
- Block內部訪問例項變數會出現的問題BloC變數
- 手機網站長時間按住頁面出現閃退問題網站
- SpringBoot整合mybatis出現時區問題Spring BootMyBatis
- 【問題&解決】手機網頁Html程式碼實現(解決顯示頁面很小的問題)網頁HTML
- 訪問jboss上EJB遠端介面問題
- 急問:mysql出現的問題MySql
- 訪問github出現無法訪問此網站Github網站
- vue傳參頁面重新整理資料丟失問題Vue
- 上傳App Store遇到的問題APP
- SpringBoot利用Nginx作為檔案伺服器實現檔案上傳和訪問Spring BootNginx伺服器