今天在用springboot2.0集成Druid的時候瞳脓,訪問http://localhost:8010/druid 的時候始終跳到下面這個界面(我設(shè)定項目工程啟動端口是8010)纫事,開始還以為這就是德魯伊的登錄界面,輸入賬號密碼一直登錄不上,http://localhost:8010/login顾腊;
image.png
猜想是不是攔截器攔截了状勤,發(fā)現(xiàn)自己在創(chuàng)建項目的時候勾選了SpringSecurity,需要在配置安全訪問時過濾路徑;需要加上如下代碼畏铆;
package com.ijustone.service.core.security;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import javax.servlet.http.HttpServletRequest;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/hello").hasRole("USER").and()
//.csrf().disable() //關(guān)閉CSRF
.csrf().requireCsrfProtectionMatcher(new RequestMatcher() {
@Override
public boolean matches(HttpServletRequest httpServletRequest) {
String servletPath = httpServletRequest.getServletPath();
if (servletPath.contains("/druid")) {
return false;
}
return true;
}
}).and()
.formLogin().loginPage("/login").defaultSuccessUrl("/hello").and()
.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
再次訪問:
image.png
就可以登錄了雷袋。