Guest User

Untitled

a guest
Nov 29th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. package com.cm.sb.service;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import com.cm.sb.model.SysUser;
  7. import com.cm.sb.repository.SysUserRepository;
  8.  
  9. @Service
  10. public class SysUserService {
  11.  
  12. @Autowired
  13. SysUserRepository sysUserRepository;
  14.  
  15. public SysUser findByusername(String username) {
  16. return sysUserRepository.findByUsername(username);
  17. }
  18.  
  19. }
  20.  
  21. package com.cm.sb;
  22.  
  23. import org.springframework.boot.SpringApplication;
  24. import org.springframework.boot.autoconfigure.SpringBootApplication;
  25. import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
  26. import org.springframework.boot.builder.SpringApplicationBuilder;
  27. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  28. import org.springframework.context.annotation.ComponentScan;
  29. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  30.  
  31. @SpringBootApplication(scanBasePackages={"com.cm.sb"},exclude = { SecurityAutoConfiguration.class })
  32. @EnableJpaRepositories(basePackages = "com.cm.sb")
  33. public class SbApplication extends SpringBootServletInitializer {
  34.  
  35. @Override
  36. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  37. return application.sources(SbApplication.class);
  38. }
  39.  
  40.  
  41. public static void main(String[] args) {
  42. SpringApplication.run(SbApplication.class, args);
  43. }
  44. }
  45.  
  46. spring.datasource.url=jdbc:mysql://localhost:3306/sb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  47. spring.datasource.username=root
  48. spring.datasource.password=
  49. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  50.  
  51. spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
  52. spring.jpa.show-sql=false
  53. spring.jpa.hibernate.ddl-auto=none
  54.  
  55. spring.mvc.view.prefix: /WEB-INF/jsp/
  56. spring.mvc.view.suffix: .jsp
  57.  
  58. security.user.password=ciro
  59. security.user.username=ciro
  60.  
  61. package com.cm.sb.config;
  62.  
  63. import javax.annotation.Resource;
  64.  
  65. import org.springframework.beans.factory.annotation.Autowired;
  66. import org.springframework.context.annotation.Bean;
  67. import org.springframework.context.annotation.Configuration;
  68. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  69. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  70. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  71. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  72. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  73. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  74. import org.springframework.security.core.userdetails.User;
  75. import org.springframework.security.core.userdetails.UserDetails;
  76. import org.springframework.security.core.userdetails.UserDetailsService;
  77. import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  78. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  79.  
  80. @Configuration
  81. @EnableWebSecurity
  82. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  83.  
  84. @Resource(name = "userDetailService")
  85. private UserDetailsService userDetailsService;
  86.  
  87. @Override
  88. protected void configure(HttpSecurity http) throws Exception {
  89.  
  90. http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("**/login"))
  91. .and().authorizeRequests().antMatchers("/dashboard").hasRole("USER")
  92. .and().authorizeRequests().antMatchers("/home").permitAll()
  93. .and().formLogin().defaultSuccessUrl("/dashboard").loginPage("/login")
  94. .and().logout().permitAll();
  95. }
  96.  
  97. @Autowired
  98. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  99. auth.userDetailsService(userDetailsService);
  100. }
  101.  
  102. @Override
  103. public void configure(WebSecurity web) throws Exception {
  104. web.ignoring().antMatchers("/*.css");
  105. web.ignoring().antMatchers("/*.js");
  106. }
  107. }
  108.  
  109. package com.cm.sb.repository;
  110.  
  111. import com.cm.sb.model.SysUser;
  112. import org.springframework.data.jpa.repository.JpaRepository;
  113. import org.springframework.stereotype.Repository;
  114.  
  115. @Repository("sysUserRepository")
  116. public interface SysUserRepository extends JpaRepository<SysUser, Integer> {
  117.  
  118. SysUser findByUsername(String username);
  119. }
Add Comment
Please, Sign In to add comment