Java tutorial
/* * Copyright 2013-2014 JIWHIZ Consulting Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.jiwhiz; import javax.inject.Inject; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; 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.authentication.preauth.AbstractPreAuthenticatedProcessingFilter; import com.jiwhiz.domain.account.UserAccountService; import com.jiwhiz.domain.account.UserRoleType; /** * Configuration for Spring Security. * * @author Yuan Ji * */ @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter { @Inject private UserAccountService userAccountService; @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable().authorizeRequests().antMatchers("/api/admin/**") .hasAuthority(UserRoleType.ROLE_ADMIN.name()).antMatchers("/api/author/**") .hasAuthority(UserRoleType.ROLE_AUTHOR.name()).antMatchers("/api/user/**") .hasAuthority(UserRoleType.ROLE_USER.name()).antMatchers("/api/public/**").permitAll().anyRequest() .authenticated().and() .addFilterBefore(testAuthenticationFilter(), AbstractPreAuthenticatedProcessingFilter.class); } @Override protected void configure(AuthenticationManagerBuilder builder) throws Exception { builder.authenticationProvider(testAuthenticationProvider()); } @Bean public TestAuthenticationProvider testAuthenticationProvider() { return new TestAuthenticationProvider(userAccountService); } @Bean public TestAuthenticationFilter testAuthenticationFilter() throws Exception { TestAuthenticationFilter filter = new TestAuthenticationFilter("/signin", authenticationManagerBean()); return filter; } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } }