You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.6 KiB

package ${basePackage}.config;
6 years ago
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5 years ago
import org.springframework.security.config.annotation.web.builders.WebSecurity;
6 years ago
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.Authentication;
5 years ago
import ${basePackage}.frame.base.Token;
import ${basePackage}.frame.utils.CookieUtil;
import ${basePackage}.frame.auth.LocalData;
5 years ago
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import javax.servlet.*;
6 years ago
import javax.servlet.http.HttpServletRequest;
5 years ago
import java.io.IOException;
5 years ago
import java.util.regex.Matcher;
import java.util.regex.Pattern;
6 years ago
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
5 years ago
@Value("${r"${web.url.auth.included}"}")
private String[] included;
6 years ago
@Value("${r"${web.url.auth.excluded}"}")
private String[] excluded;
5 years ago
@Value("${r"${spring.mvc.static-path-pattern}"}")
private String[] staticPath;
6 years ago
5 years ago
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().mvcMatchers(staticPath);
}
6 years ago
@Override
protected void configure(HttpSecurity http) throws Exception {
5 years ago
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(new TokenFilter(), FilterSecurityInterceptor.class)// 过滤器用于处理Token
6 years ago
.authorizeRequests()
5 years ago
.antMatchers(excluded).permitAll()// 放行排除的URL
.antMatchers(included).access("@Authorization.hasPermission(request,authentication)")// 需要权限的URL
6 years ago
.and().cors()
.and().headers().frameOptions().disable()
.and().csrf().disable();
}
/**
*
5 years ago
* <p>
6 years ago
* Using generated security password: f6b42a66-71b1-4c31-b6a8-942838c81408
*
* @return
* @throws Exception
*/
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
5 years ago
public static class TokenFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
String token = request.getParameter("token");
if (token == null || token.isEmpty()) {
token = CookieUtil.getCookieValue(request.getCookies(), "token");
}
if (token == null) {
LocalData.setToken(LocalData.getTempToken());
} else {
// 组装Token ~ 这边根据实际的业务组装Token
Token token1 = new Token();
token1.setId(1L);
token1.setUserId(1L);
token1.setUserName("admin");
//继承临时Token
token1.addResourceSet(LocalData.getTempToken());
//管理员特有资源(这边请用正则表达式)
token1.putResource(".*");
LocalData.setToken(token1);
}
5 years ago
// Action
String servletPath = request.getServletPath();
Pattern compile = Pattern.compile("^/(.+)\\.htm");
Matcher matcher = compile.matcher(servletPath);
if (matcher.find()) {
LocalData.setAction(matcher.group(1));
}
5 years ago
filterChain.doFilter(servletRequest, servletResponse);
}
}
6 years ago
@Bean("Authorization")
public Object getAuthorization() {
return new Object() {
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
// 授权
Token token_ = LocalData.getToken();
if (token_.hasResource(request.getServletPath())) {
return true;
}
return false;
}
};
}
}

Powered by TurnKey Linux.