Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- @EnableWebSecurity
- @EnableJpaRepositories(basePackageClasses = UserRepository.class)
- @Configuration
- public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
- @Autowired
- private CustomUserDetailsService userDetailsService;
- @Override
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(userDetailsService)
- .passwordEncoder(getPasswordEncoder());
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.csrf().disable();
- http.authorizeRequests()
- .antMatchers("**/secured/**").authenticated()
- .anyRequest().permitAll()
- .and()
- .formLogin().loginPage("/loginPage")
- .usernameParameter("username").passwordParameter("password")
- .permitAll();
- }
- private PasswordEncoder getPasswordEncoder() {
- return new PasswordEncoder() {
- @Override
- public String encode(CharSequence charSequence) {
- return charSequence.toString();
- }
- @Override
- public boolean matches(CharSequence charSequence, String s) {
- return true;
- }
- };
- }
- }
- public class WelcomeController {
- @Autowired
- private UserRepository userRepository;
- @RequestMapping("/w")
- public String welcome(ModelMap map) {
- map.put("currentDate", new Date());
- return "welcome";
- }
- @RequestMapping("/loginPage")
- public String getLoginPage(ModelMap map) {
- System.out.println("geberrich loginpage");
- return "loginPage";
- }
- @RequestMapping(value= "/add",method= RequestMethod.GET) // Map ONLY GET Requests
- public @ResponseBody String addNewUser (@RequestParam(value="t1") String name
- , @RequestParam(value="t2") String email,@RequestParam(value="t3") String password) {
- // @ResponseBody means the returned String is the response, not a view name
- // @RequestParam means it is a parameter from the GET or POST request
- //build roles
- Set roles= new HashSet<Role>();
- Role role= new Role();
- role.setRole("ADMI");
- roles.add(role);
- //add user
- User n = new User();
- n.setName(name);
- n.setEmail(email);
- n.setPassword(password);
- n.setRoles(roles);
- userRepository.save(n);
- return "Saved";
- }
- @PreAuthorize("hasAnyRole('ADMI')")
- @RequestMapping(value= "/secured/all",method= RequestMethod.GET)
- public @ResponseBody String securedHello() {
- System.out.println("geberrich secured all");
- return "Secured Hello";
- }
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- </head>
- <body>
- <h3>Login with user name and password</h3>
- <form >
- <label for="username">Username</label>
- <input type="text" name="username" id="username"><br>
- <label for="password">Password</label>
- <input type="password" id="password" name="password"/><br>
- <button type="submit"> Login</button><br>
- </body>
- </html>
- 2017-12-18 13:16:50.372 WARN 5804 --- [nio-8082-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type [java.lang.String] to required type [int]; nested exception is java.lang.NumberFormatException: For input string: "robots"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement