Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. @RequestMapping(value = "/simple", method = RequestMethod.POST)
  2. @ResponseBody
  3. @Transactional
  4. @Preauthorize(...)
  5. public String simple(){
  6. //collect the user's current details from the getPrinciple() and complete the transaction...
  7. Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  8. return "Simple";
  9. }
  10.  
  11. <beans xmlns="http://www.springframework.org/schema/beans"
  12. xmlns:security="http://www.springframework.org/schema/security"
  13. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  14. xsi:schemaLocation="http://www.springframework.org/schema/beans
  15. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  16. http://www.springframework.org/schema/security
  17. http://www.springframework.org/schema/security/spring-security-3.2.xsd">
  18.  
  19. <security:global-method-security
  20. secured-annotations="enabled" />
  21.  
  22. <security:http pattern="/**"
  23. auto-config="true" disable-url-rewriting="true" use-expressions="true">
  24. <security:custom-filter ref="authenticationTokenProcessingFilter"
  25. position="FORM_LOGIN_FILTER" />
  26. <security:intercept-url pattern="/authenticate"
  27. access="permitAll" />
  28. <security:intercept-url pattern="/secure/**"
  29. access="isAuthenticated()" />
  30. </security:http>
  31.  
  32. <bean id="CustomAuthenticationEntryPoint" class="org.foo.CustomAuthenticationEntryPoint" />
  33.  
  34. <bean class="org.foo.AuthenticationTokenProcessingFilter" id="authenticationTokenProcessingFilter">
  35. <constructor-arg ref="authenticationManager" />
  36. </bean>
  37.  
  38. </beans>
  39.  
  40. @Bean
  41. public ApplicationSecurity applicationSecurity() {
  42. return new ApplicationSecurity();
  43. }
  44.  
  45. @Order(Ordered.LOWEST_PRECEDENCE - 8)
  46. protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
  47. @Override
  48. protected void configure(HttpSecurity http) throws Exception {
  49. // this is obviously for a simple "login page" not a custom filter!
  50. http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin()
  51. .loginPage("/login").failureUrl("/login?error").permitAll();
  52. }
  53. }
  54.  
  55. **@EnableGlobalMethodSecurity**
  56.  
  57. import javax.servlet.Filter;
  58.  
  59. import org.springframework.beans.factory.annotation.Autowired;
  60. import org.springframework.beans.factory.annotation.Qualifier;
  61. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  62. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  63. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  64. import org.springframework.security.web.AuthenticationEntryPoint;
  65. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  66.  
  67. @EnableGlobalMethodSecurity(securedEnabled=true) //<security:global-method-security secured-annotations="enabled" />
  68. public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
  69.  
  70. @Autowired
  71. @Qualifier("authenticationTokenProcessingFilter")
  72. private Filter authenticationTokenProcessingFilter;
  73.  
  74. @Autowired
  75. private AuthenticationEntryPoint entryPoint;
  76.  
  77. @Override
  78. protected void configure(HttpSecurity http) throws Exception {
  79. http.exceptionHandling().authenticationEntryPoint(entryPoint);
  80.  
  81.  
  82. http //auto-config="true"
  83. .authorizeRequests()
  84. .anyRequest().authenticated()
  85. .and()
  86. .formLogin()
  87. .and()
  88. .httpBasic();
  89.  
  90.  
  91. http
  92. .authorizeRequests() // use-expressions="true"
  93. .antMatchers("/authenticate").permitAll() //<security:intercept-url pattern="/authenticate" access="permitAll" />
  94. .antMatchers("/secure/**").authenticated() //<security:intercept-url pattern="/secure/**" access="isAuthenticated()" />
  95. .and()
  96. .addFilterBefore(authenticationTokenProcessingFilter, UsernamePasswordAuthenticationFilter.class) // <security:custom-filter ref="authenticationTokenProcessingFilter" position="FORM_LOGIN_FILTER" /> http://docs.spring.io/spring-security/site/docs/3.0.x/reference/ns-config.html
  97. ;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement