(118) - Add java-config(零配置) 的支持

2.0.0
Li Shengzhao 2016-04-03 23:51:13 +08:00
parent 4d94ac10c6
commit d7acfccd5c
3 changed files with 66 additions and 4 deletions

View File

@ -23,12 +23,12 @@ public class ServletInitializer extends AbstractAnnotationConfigDispatcherServle
@Override
protected Class<?>[] getRootConfigClasses() {
throw new UnsupportedOperationException("Not yet implemented");
return new Class[]{WebSecurityConfigurer.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{MkkWebMvcConfigurer.class};
return new Class[]{WebMvcConfigurer.class};
}
@Override
@ -43,7 +43,7 @@ public class ServletInitializer extends AbstractAnnotationConfigDispatcherServle
// servletContext.setAttribute("webAppRootKey", "spring-oauth-server");
servletContext.setInitParameter("webAppRootKey", "spring-oauth-server");
servletContext.setInitParameter("contextConfigLocation", "classpath:spring/*.xml");
// servletContext.setInitParameter("contextConfigLocation", "classpath:spring/*.xml");
servletContext.setInitParameter("log4jConfigLocation", "/WEB-INF/log4j.xml");
//Add Filters

View File

@ -1,5 +1,6 @@
package com.monkeyk.sos.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@ -14,7 +15,8 @@ import org.springframework.web.servlet.view.JstlView;
*/
@Configuration
@EnableWebMvc
public class MkkWebMvcConfigurer extends WebMvcConfigurerAdapter {
@ComponentScan(basePackages = {"com.monkeyk.sos.web"})
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

View File

@ -0,0 +1,60 @@
package com.monkeyk.sos.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
/**
* 2016/4/3
* <p/>
* Replace security.xml
*
* @author Shengzhao Li
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.expressionHandler(new OAuth2WebSecurityExpressionHandler());
web.ignoring().antMatchers("/resources/**");
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/**").hasAnyRole("ROLE_USER,ROLE_UNITY,ROLE_MOBILE")
.antMatchers("/**").anonymous()
.and()
.exceptionHandling().accessDeniedPage("/login.jsp?authorization_error=2")
.and()
.csrf().disable()
.formLogin().loginPage("/login.jsp")
.failureUrl("/login.jsp?authentication_error=1")
.defaultSuccessUrl("/index.jsp")
.loginProcessingUrl("/login.do")
.and()
.logout().logoutUrl("/logout.do")
.logoutSuccessUrl("/index.jsp")
.and()
.anonymous();
}
}