View Javadoc
1   package org.oxerr.spring.security.wechat.samples.helloworld.web;
2   
3   import static org.springframework.web.bind.annotation.RequestMethod.GET;
4   
5   import java.io.IOException;
6   import java.nio.charset.StandardCharsets;
7   import java.security.Principal;
8   
9   import javax.annotation.security.RolesAllowed;
10  import javax.servlet.http.HttpServletResponse;
11  
12  import org.oxerr.spring.security.wechat.samples.helloworld.service.impl.WeChatUserDetails;
13  import org.springframework.security.core.Authentication;
14  import org.springframework.stereotype.Controller;
15  import org.springframework.web.bind.annotation.RequestMapping;
16  
17  import com.foxinmy.weixin4j.mp.model.User;
18  
19  @Controller
20  @RolesAllowed("ROLE_USER")
21  public class IndexController {
22  
23  	@RequestMapping(method = GET, path = "/")
24  	public void getIndex(
25  		Principal principal,
26  		Authentication authentication,
27  		HttpServletResponse response
28  	) throws IOException {
29  
30  		User user = ((WeChatUserDetails) authentication.getPrincipal()).getUser();
31  		String nickname = user.getNickName();
32  
33  		response.setCharacterEncoding(StandardCharsets.UTF_8.name());
34  		response.setContentType("text/plain");
35  		response.getWriter().println("hello " + nickname);
36  		response.flushBuffer();
37  	}
38  
39  }