forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9a91f0
commit 7a35c66
Showing
5 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
48 changes: 45 additions & 3 deletions
48
spring-security-rest-full/src/main/java/org/baeldung/spring/SecSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,58 @@ | ||
package org.baeldung.spring; | ||
|
||
import org.baeldung.web.error.CustomAccessDeniedHandler; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.ImportResource; | ||
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.builders.WebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
|
||
@Configuration | ||
@EnableAutoConfiguration | ||
@ImportResource({ "classpath:webSecurityConfig.xml" }) | ||
public class SecSecurityConfig { | ||
// | ||
@EnableWebSecurity | ||
@EnableGlobalMethodSecurity(prePostEnabled = true) | ||
// @ImportResource({ "classpath:webSecurityConfig.xml" }) | ||
public class SecSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
@Autowired | ||
private CustomAccessDeniedHandler accessDeniedHandler; | ||
|
||
public SecSecurityConfig() { | ||
super(); | ||
} | ||
|
||
// java config | ||
|
||
@Override | ||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception { | ||
auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER").and().withUser("admin").password("adminPass").authorities("ROLE_ADMIN"); | ||
} | ||
|
||
@Override | ||
public void configure(final WebSecurity web) throws Exception { | ||
web.ignoring().antMatchers("/resources/**"); | ||
} | ||
|
||
@Override | ||
protected void configure(final HttpSecurity http) throws Exception { | ||
// @formatter:off | ||
http | ||
.csrf().disable() | ||
.authorizeRequests() | ||
.antMatchers("/admin/*").hasAnyRole("ROLE_ADMIN") | ||
.anyRequest().authenticated() | ||
.and() | ||
.httpBasic() | ||
.and() | ||
// .exceptionHandling().accessDeniedPage("/my-error-page") | ||
.exceptionHandling().accessDeniedHandler(accessDeniedHandler) | ||
; | ||
// @formatter:on | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ng-security-rest-full/src/main/java/org/baeldung/web/error/CustomAccessDeniedHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.baeldung.web.error; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CustomAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(final HttpServletRequest request, final HttpServletResponse response, final AccessDeniedException ex) throws IOException, ServletException { | ||
response.getOutputStream().print("Error Message Goes Here"); | ||
// response.sendRedirect("/my-error-page"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters