Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. <beans:beans xmlns="http://www.springframework.org/schema/security"
  2. xmlns:beans="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/security
  8. http://www.springframework.org/schema/security/spring-security-4.1.xsd">
  9.  
  10. <http auto-config="true" use-expressions="true">
  11. <intercept-url pattern="/admin**" access="isAuthenticated()" />
  12. <intercept-url pattern="/admin/*" access="isAuthenticated()"/>
  13. <form-login
  14. login-page="/login"
  15. default-target-url="/"
  16. login-processing-url="/j_spring_security_check"
  17. authentication-failure-url="/login?error"
  18. username-parameter="username"
  19. password-parameter="password" />
  20. <logout logout-success-url="/login?logout" />
  21. <!--<csrf/>-->
  22. <http-basic/>
  23. </http>
  24.  
  25. <authentication-manager>
  26. <authentication-provider ref="authenticationProvider"/>
  27. </authentication-manager>
  28.  
  29. @Component(value = "authenticationProvider")
  30. public class NiceAuthenticationProvider implements AuthenticationProvider {
  31.  
  32. @Autowired
  33. private IUserService userService;
  34.  
  35. @Override
  36. public Authentication authenticate(Authentication authentication) throws AuthenticationException{
  37. String username=authentication.getName();
  38. String password=(String)authentication.getCredentials();
  39. User user = userService.getUserByUsername(username);
  40. if(user==null)
  41. throw new BadCredentialsException("User not found");
  42. if(!password.equals(user.getPassword()))
  43. throw new BadCredentialsException("Wrong password");
  44.  
  45. Collection<?extends GrantedAuthority> authorities = user.getRoles();
  46.  
  47. return new UsernamePasswordAuthenticationToken(user, password, authorities);
  48.  
  49. }
  50.  
  51. @Override
  52. public boolean supports(Class<?> aClass) {
  53. return true;
  54. }
  55.  
  56. public void setService(UserService service) {
  57. this.userService = service;
  58. }
  59.  
  60. public IUserService getUserService() {
  61. return userService;
  62. }
  63. }
  64.  
  65. @Configuration
  66. @EnableWebSecurity
  67. @ComponentScan("com.websystique.springmvc")
  68. @Order(1)
  69. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  70. @Autowired
  71. NiceAuthenticationProvider niceAuthenticationProvider;
  72.  
  73. @Override
  74. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  75. auth.authenticationProvider(niceAuthenticationProvider);
  76. }
  77.  
  78. @Override
  79. protected void configure(HttpSecurity http) throws Exception {
  80. http
  81. .authorizeRequests().anyRequest().authenticated()
  82. .and()
  83. .httpBasic();
  84. }
  85. }
  86.  
  87. @Configuration
  88. @EnableWebMvc
  89. @ComponentScan(basePackages = "com.websystique.springmvc")
  90.  
  91.  
  92. public class AppConfig {
  93.  
  94. @Bean
  95. public ViewResolver viewResolver() {
  96. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  97. viewResolver.setViewClass(JstlView.class);
  98. viewResolver.setPrefix("/WEB-INF/views/");
  99. viewResolver.setSuffix(".jsp");
  100.  
  101. return viewResolver;
  102. }
  103.  
  104. @Bean
  105. public MessageSource messageSource() {
  106. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  107. messageSource.setBasename("messages");
  108. return messageSource;
  109. }
  110. }
  111.  
  112. @Service("userService")
  113. @Transactional
  114. public class UserService implements IUserService{
  115. @Autowired
  116. private UsersDao usersDao;
  117.  
  118. public User getUserByUsername(String username){
  119. return usersDao.findUserByUserName(username);
  120. }
  121.  
  122. public void setUsersDao(UsersDaoImpl usersDao) {
  123. this.usersDao = usersDao;
  124. }
  125. }
  126.  
  127. @Repository("usersDao")
  128. public class UsersDaoImpl extends AbstractDao<Integer, User> implements UserDetailsService, UsersDao {
  129.  
  130. public User findUserByUserName(String username) throws UsernameNotFoundException {
  131. Criteria criteria = createEntityCriteria();
  132. criteria.add(Restrictions.eq("username", username));
  133. User usr= (User) criteria.uniqueResult();
  134. if(usr!=null)
  135. return usr;
  136. else
  137. throw new UsernameNotFoundException("USER NOT FOUND");
  138. }
  139. @Override
  140. public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
  141. return new CustomUserDetails(findUserByUserName(s));
  142. }
  143. }
  144.  
  145. public abstract class AbstractDao<PK extends Serializable, T> {
  146. private final Class<T> persistentClass;
  147.  
  148. @SuppressWarnings("unchecked")
  149. public AbstractDao(){
  150. this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
  151. }
  152.  
  153. @Autowired
  154. private SessionFactory sessionFactory;
  155.  
  156. protected Session getSession(){
  157. return sessionFactory.getCurrentSession();
  158. }
  159.  
  160. @SuppressWarnings("unchecked")
  161. public T getByKey(PK key) {
  162. return (T) getSession().get(persistentClass, key);
  163. }
  164.  
  165. public void persist(T entity) {
  166. getSession().persist(entity);
  167. }
  168.  
  169. public void delete(T entity) {
  170. getSession().delete(entity);
  171. }
  172.  
  173. protected Criteria createEntityCriteria(){
  174. return getSession().createCriteria(persistentClass);
  175. }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement