Created
December 19, 2016 18:45
-
-
Save netodevel/4fd70e625eaa48be2144338218dd7df5 to your computer and use it in GitHub Desktop.
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
package com.baeldung.spring.security.x509; | |
import java.security.Principal; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.security.access.prepost.PreAuthorize; | |
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.configuration.EnableWebSecurity; | |
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.authority.AuthorityUtils; | |
import org.springframework.security.core.userdetails.User; | |
import org.springframework.security.core.userdetails.UserDetails; | |
import org.springframework.security.core.userdetails.UserDetailsService; | |
import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
@SpringBootApplication | |
@EnableWebSecurity | |
@EnableGlobalMethodSecurity(prePostEnabled = true) | |
@Controller | |
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter { | |
public static void main(String[] args) { | |
SpringApplication.run(X509AuthenticationServer.class, args); | |
} | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http.authorizeRequests().anyRequest().authenticated().and().x509().userDetailsService(userDetailsService()); | |
} | |
@PreAuthorize("hasAuthority('ROLE_USER')") | |
@RequestMapping(value = "/user", method = RequestMethod.GET) | |
public String user(Model model, Principal principal) { | |
UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal(); | |
model.addAttribute("username", currentUser.getUsername()); | |
return "user"; | |
} | |
@RequestMapping(value = "/") | |
@ResponseBody | |
public String index() { | |
return "index"; | |
} | |
@Bean | |
public UserDetailsService userDetailsService() { | |
return new UserDetailsService() { | |
@Override | |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | |
if (username.equals("cid")) { | |
return new User(username, "", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); | |
} | |
throw new UsernameNotFoundException("User not found!"); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment